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?
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.
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