Issue
I create a sketch to create an extrusion. An error is occurring when using the sketch to create a profile. The error occurs on this line. Why am I getting the error?
Set oProfile1 = tySketch.Profiles.AddForSolid"
Here is the VBA code I am using:
Sub Test()
Dim App As Inventor.Application
Set App = ThisApplication
' this code will only work on an active part
Dim Part1 As PartDocument
Set Part1 = ThisApplication.ActiveDocument
Dim TG As TransientGeometry
Set TG = App.TransientGeometry
'try to read face with transient id 1
Dim oFace As Face
Set oFace = Part1.ComponentDefinition.SurfaceBodies(1).BindTransientKeyToObject(1)
Dim tySketch As PlanarSketch
Dim oWorkPlane As WorkPlane
Set oWorkPlane = Part1.ComponentDefinition.WorkPlanes.AddFixed(TG.CreatePoint
(oFace.Evaluator.RangeBox.MinPoint.X, 0, 0), TG.CreateUnitVector(0, 1, 0), TG.CreateUnitVector(0, 0, 1),
False)
Set oPartCompDef = Part1.ComponentDefinition
Set tySketch = oPartCompDef.Sketches.Add(oWorkPlane)
' create some temporary variables for drawing lines
Dim tyStartPoint As Point2d
Dim tyEndPoint As Point2d
Dim tyTmpPoint As Point2d
Dim tyLine As SketchLine
Set tyStartPoint = TG.CreatePoint2d(0, 0)
Set tyEndPoint = TG.CreatePoint2d(0, 0)
Set tyTmpPoint = TG.CreatePoint2d(0, 0)
'draw some lines to create a shap fo the extrusion
Set tyStartPoint = tySketch.ModelToSketchSpace(oFace.Evaluator.RangeBox.MinPoint)
Set tyEndPoint = tySketch.ModelToSketchSpace(oFace.Evaluator.RangeBox.MaxPoint)
tyTmpPoint.X = tyStartPoint.X
tyTmpPoint.Y = tyEndPoint.Y
Set tyLine = tySketch.SketchLines.AddByTwoPoints(tyStartPoint, tyTmpPoint)
Set tyLine = tySketch.SketchLines.AddByTwoPoints(tyTmpPoint, tyEndPoint)
Set tyLine = tySketch.SketchLines.AddByTwoPoints(tyEndPoint, tyStartPoint)
' try to create a profile
Dim oProfile1 As Profile
Set oProfile1 = tySketch.Profiles.AddForSolid
' Do an extrusion on the solid
oExtrusion = oPartCompDef.Features.ExtrudeFeatures.AddByDistanceExtent(oProfile, 3, kSymmetricExtentDirection, kJoinOperation, Null)
End Sub
Solution
The error occurs because there are no constraints between the SketchLines. You can use SketchPoint.Merge to add the constraints. You can also use a SketchPoint directly as an argument when the Sketch geometry is being created.
Set oFirstline = tySketch.SketchLines.AddByTwoPoints(¡¬¡¬)
Set oSecondLine = tySketch.SketchLines.AddByTwoPoints(¡¬¡¬)
Set oLastline = tySketch.SketchLines.AddByTwoPoints(¡¬¡¬)
Call oFirstline.EndSketchPoint.Merge(
oSecondLine.StartSketchPoint)
Call oSecondLine.EndSketchPoint.Merge(
oLastline.StartSketchPoint)
Call oFirstline.StartSketchPoint.Merge(
oLastline.EndSketchPoint)
Here is a VB.NET example that creates a sketch, uses SketchPoint.Merge and then creates an extrusion. Before running this code create a part with rectangular faces like a box. This example is from a button on a form in a standalone exe that connects to Inventor from out of process.
Imports Inventor
Public Class Form1
Dim m_inventorApp As Inventor.Application = Nothing
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Try ' Try to get an active instance of Inventor
Try
m_inventorApp = System.Runtime.InteropServices.
Marshal.GetActiveObject("Inventor.Application")
Catch ' If not active, create a new Inventor session
Dim inventorAppType As Type = System.Type.
GetTypeFromProgID("Inventor.Application")
m_inventorApp = System.Activator.CreateInstance(
inventorAppType)
'Must be set visible explicitly
m_inventorApp.Visible = True
End Try
Catch
System.Windows.Forms.MessageBox.Show(
"Error: couldn't create Inventor instance")
End Try
Test()
End Sub
Sub Test()
Try
' this code will only work on an active part
Dim Part1 As PartDocument
Part1 = m_inventorApp.ActiveDocument
Dim TG As TransientGeometry
TG = m_inventorApp.TransientGeometry
'try to read face with transient id 1
Dim oFace As Face
oFace = Part1.ComponentDefinition.
SurfaceBodies(1).BindTransientKeyToObject(1)
Dim tySketch As PlanarSketch
Dim oWorkPlane As WorkPlane
oWorkPlane = Part1.ComponentDefinition.WorkPlanes.
AddFixed(TG.CreatePoint _
(oFace.Evaluator.RangeBox.MinPoint.X, 0, 0),
TG.CreateUnitVector(0, 1, 0), _
TG.CreateUnitVector(0, 0, 1), False)
Dim oPartCompDef As PartComponentDefinition
oPartCompDef = Part1.ComponentDefinition
tySketch = oPartCompDef.Sketches.Add(oWorkPlane)
'draw some lines to create a shape for the extrusion
Dim MinPoint As Point2d = tySketch.
ModelToSketchSpace(
oFace.Evaluator.RangeBox.MinPoint)
Dim MaxPoint As Point2d = tySketch.
ModelToSketchSpace(
oFace.Evaluator.RangeBox.MaxPoint)
Dim Pnt2 As Point2d = TG.CreatePoint2d(
MaxPoint.X, MinPoint.Y)
Dim tyLine1 As SketchLine = tySketch.SketchLines.
AddByTwoPoints(MinPoint, Pnt2)
Dim tyLine2 As SketchLine = tySketch.SketchLines.
AddByTwoPoints(Pnt2, MaxPoint)
tyLine1.EndSketchPoint.Merge(
tyLine2.StartSketchPoint)
Dim Pnt3 As Point2d = TG.CreatePoint2d(M
inPoint.X, MaxPoint.Y)
Dim tyLine3 As SketchLine = tySketch.SketchLines.
AddByTwoPoints(MaxPoint, Pnt3)
tyLine2.EndSketchPoint.Merge(
tyLine3.StartSketchPoint)
Dim tyLine4 As SketchLine = tySketch.SketchLines.
AddByTwoPoints(Pnt3, MinPoint)
tyLine3.EndSketchPoint.Merge(
tyLine4.StartSketchPoint)
tyLine4.EndSketchPoint.Merge(
tyLine1.StartSketchPoint)
' try to create a profile
Dim oProfile1 As Profile
oProfile1 = tySketch.Profiles.AddForSolid
' Do an extrusion on the solid
Dim oExtrusion As ExtrudeFeature
oExtrusion = oPartCompDef.Features.ExtrudeFeatures.
AddByDistanceExtent(oProfile1, 3, _
PartFeatureExtentDirectionEnum.kSymmetricExtentDirection, _
PartFeatureOperationEnum.kJoinOperation, Nothing)
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
End Class