Can't Create a Profile using API

Can't Create a Profile using API

Boopathi_Sivakumar
Collaborator Collaborator
1,569 Views
7 Replies
Message 1 of 8

Can't Create a Profile using API

Boopathi_Sivakumar
Collaborator
Collaborator

Hello Guys,

        I have started to learn API, I have created a code that will create a rectangle it creates with no issues but when i tried to create a extrude it says no Valid region.

In Another method i have created a rectangle with SketchEntitiesEnumerator  and that profile have nos issues.

 

I think i have missed something kindly guide me through

I have attached my program and also i have attached my exe too

ERROR.PNG

Imports System
Imports System.Type
Imports System.Activator
Imports System.Runtime.InteropServices
Imports Inventor


Public Class Form1

    Dim _InvApp As Inventor.Application
    Dim Odoc As Document
    Dim Opcd As PartComponentDefinition
    Dim Otg As TransientGeometry
    Dim Oske As PlanarSketch

    
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        Try
            _InvApp = Marshal.GetActiveObject("Inventor.Application")

        Catch ex As Exception
            Try
                Dim Invapptype As Type = GetTypeFromProgID("Inventor.Application")
                _InvApp = CreateInstance(Invapptype)
                _InvApp.Visible = True

            Catch ex1 As Exception


            End Try

        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Odoc = _InvApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject, _InvApp.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject))
    End Sub

    Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        '  Odoc.Close(SkipSave:=True)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        If Not TypeOf _InvApp.ActiveDocument Is PartDocument Then
            MsgBox("Need to Create a Part")
            MsgBox("Wish to create a Part", MsgBoxStyle.YesNo)

            Odoc = _InvApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject, _InvApp.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject))
            If Not TypeOf _InvApp.ActiveEditObject Is PlanarSketch Then
                MsgBox("Need to Add a Sketch")
                MsgBox("Wish to create a sketch", MsgBoxStyle.YesNo)

                Opcd = _InvApp.ActiveDocument.ComponentDefinition
                Oske = Opcd.Sketches.Add(Opcd.WorkPlanes.Item(3))
                Otg = _InvApp.TransientGeometry

                Dim Lin(3) As SketchLine
                Dim HL As Decimal = (InputBox("Enter The Length of the Rectangle: ")) / 10
                Dim VL As Decimal = (InputBox("Enter The Height of the Rectangle: ")) / 10


                Lin(0) = Oske.SketchLines.AddByTwoPoints(Otg.CreatePoint2d(0, 0), Otg.CreatePoint2d(HL, 0))
                Lin(1) = Oske.SketchLines.AddByTwoPoints(Lin(0).EndSketchPoint, Otg.CreatePoint2d(HL, VL))
                Lin(2) = Oske.SketchLines.AddByTwoPoints(Lin(1).EndSketchPoint, Otg.CreatePoint2d(0, VL))
                Lin(3) = Oske.SketchLines.AddByTwoPoints(Lin(2).EndSketchPoint, Otg.CreatePoint2d(0, 0))

                Call Oske.GeometricConstraints.AddGround(Lin(0).StartSketchPoint)
                Call Oske.GeometricConstraints.AddHorizontal(Lin(0))
                Call Oske.GeometricConstraints.AddVertical(Lin(1))
                Call Oske.GeometricConstraints.AddHorizontal(Lin(2))
                Call Oske.GeometricConstraints.AddVertical(Lin(3))

                Call Oske.DimensionConstraints.AddTwoPointDistance(Lin(0).StartSketchPoint, Lin(0).EndSketchPoint, DimensionOrientationEnum.kHorizontalDim, Otg.CreatePoint2d(0, -1))
                Call Oske.DimensionConstraints.AddTwoPointDistance(Lin(1).StartSketchPoint, Lin(1).EndSketchPoint, DimensionOrientationEnum.kVerticalDim, Otg.CreatePoint2d(6, 0))
                Call Oske.GeometricConstraints.AddEqualLength(Lin(1), Lin(3))
                Call Oske.GeometricConstraints.AddEqualLength(Lin(0), Lin(2))


                Dim pro1 As Profile
                pro1 = Oske.Profiles.AddForSolid

            End If
        End If



    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        If Not TypeOf _InvApp.ActiveDocument Is PartDocument Then
            MsgBox("Need to Create a Part")
            MsgBox("Wish to create a Part", MsgBoxStyle.YesNo)

            Odoc = _InvApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject, _InvApp.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject))
            If Not TypeOf _InvApp.ActiveEditObject Is PlanarSketch Then
                MsgBox("Need to Add a Sketch")
                MsgBox("Wish to create a sketch", MsgBoxStyle.YesNo)

                Opcd = _InvApp.ActiveDocument.ComponentDefinition
                Oske = Opcd.Sketches.Add(Opcd.WorkPlanes.Item(3))
                Otg = _InvApp.TransientGeometry

                Dim rec As SketchEntitiesEnumerator

                rec = Oske.SketchLines.AddAsTwoPointRectangle(Otg.CreatePoint2d(0, 0), Otg.CreatePoint2d(5, 5))
            End If

        Else

            Dim rec As SketchEntitiesEnumerator

            rec = Oske.SketchLines.AddAsTwoPointRectangle(Otg.CreatePoint2d(0, 0), Otg.CreatePoint2d(5, 5))

        End If


    End Sub
End Class

 

Thanks,

Boopathi

 

 

 

Boopathi Sivakumar
Sr Application Engineer
www.usamcadsoft.in
Facebook | Twitter | LinkedIn

0 Likes
Accepted solutions (2)
1,570 Views
7 Replies
Replies (7)
Message 2 of 8

AlexFielder
Advisor
Advisor

This iLogic rule of mine should help (code below):

 

Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Public Sub Main()
If ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
	Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Auto Extrude")
	Try
	ExtrudeFeature(ThisApplication.ActiveDocument)
	trans.End()
	Catch ex As Exception
		trans.Abort
		MessageBox.Show("the error was: " & ex.Message)
	End Try
	'Draw_Rectangle()
Else
	MsgBox("This rule does not work in an Assembly!")
End If
'not implemented yet=
'RunForm()
'CreateGeometry
End Sub

'''This method is cobbled together mostly from the API help.
'''
'''
'''
Public Sub ExtrudeFeature(ByVal ActiveDoc As Document)
	
	Dim StartTime As DateTime = Now
	Dim ElapsedTime As TimeSpan
	
    ' Create a new part document, using the default part template.
    'ONLY REQUIRED IF WE WANT TO CREATE A NEW PART FILE
	'Dim oPartDoc As PartDocument = ThisApplication.Documents.Add(kPartDocumentObject, ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))

    ' Set a reference to the component definition.
    'Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
	'Gets the activedocument componentdefinition.
	Dim oCompDef As PartComponentDefinition = ActiveDoc.ComponentDefinition
	
    ' Create a new sketch on the X-Y work plane.
    Dim Sketch1 As PlanarSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))

    ' Set a reference to the transient geometry object.
    Dim oTransGeom As TransientGeometry = ThisApplication.TransientGeometry

	' Check to see if there's a point in the sketch that represents the origin.
    Dim oOriginWP As WorkPoint  = oCompDef.WorkPoints.Item(1)
    'set as global above
    Dim oOriginSketchPoint As SketchPoint
    For Each oSketchPoint As SketchPoint In Sketch1.SketchPoints
        If oSketchPoint.ReferencedEntity Is oOriginWP Then
            oOriginSketchPoint = oSketchPoint
            Exit For
        End If
    Next
   
    ' Create the sketch point for the origin, if it doesn't already exist.
    If oOriginSketchPoint Is Nothing Then
        ' Project the origin point onto the sketch.
        oOriginSketchPoint = Sketch1.AddByProjectingEntity(oCompDef.WorkPoints.Item(1))
    End If
	
    ' Draw a 4cm x 3cm rectangle with the corner at (0,0)
	'NEED TO USE LENGTH AND WIDTH PARAMETERS HERE:
	'THE PARAMETER VALUES ARE METRIC mm NOT cm AS EXPECTED BY THE CODE.
	Dim olength As Double = Parameter("Length") /10
	Dim owidth As Double = Parameter("Width") /10
	Dim oRectangleLines As SketchEntitiesEnumerator = Sketch1.SketchLines.AddAsTwoPointCenteredRectangle(oOriginSketchPoint.Geometry, oTransGeom.CreatePoint2d(olength / 2, owidth / 2))
    'Dim oRectangleLines As SketchEntitiesEnumerator = oSketch.SketchLines.AddAsTwoPointCenteredRectangle(oTransGeom.CreatePoint2d(0, 0), oTransGeom.CreatePoint2d(olength / 2, owidth / 2))
	
	'ADD COINCIDENT CONSTRAINT BETWEEN RECTANGLE CENTRE AND ORIGIN
	Dim oRectangCentrePnt As SketchPoint = Sketch1.SketchPoints(6)
	Sketch1.GeometricConstraints.AddCoincident( oRectangCentrePnt,oOriginSketchPoint)
	debugrectanglelines(oRectangleLines, StartTime)
	
	'null oOriginSketchPoint so we can reassign it later
	oOriginSketchPoint = Nothing
	
	'NEED TO ITERATE THROUGH THE CREATED LINES AND ADD DIMENSIONS/CONSTRAINTS WHERE APPROPRIATE
	
'	For Each oLine In oRectangleLines
	
'ADD CONSTRAINTS TO FIRST SKETCH:
	Dim oConstraint As TwoPointDistanceDimConstraint
	
	Dim TextPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(0, ((Parameter("Width") /10)+10) / 2)
	oConstraint = Sketch1.DimensionConstraints.AddTwoPointDistance(oRectangleLines(1).EndSketchPoint, oRectangleLines(3).EndSketchPoint, DimensionOrientationEnum.kHorizontalDim, TextPoint, False)
	oConstraint.Parameter.Expression = "Width"
	TextPoint = ThisApplication.TransientGeometry.CreatePoint2d(- ((Parameter("Length") / 10) / 2) - 10, 0)
	oConstraint = Sketch1.DimensionConstraints.AddTwoPointDistance(oRectangleLines(2).EndSketchPoint, oRectangleLines(4).EndSketchPoint, DimensionOrientationEnum.kVerticalDim, TextPoint, False)
	oConstraint.Parameter.Expression = "Length"
'	oRectangleLines(1)
'	oRectangleLines(3)
'	oRectangleLines(2)
'	oRectangleLines(4)

    ' Create a profile.
    Dim oProfile As Profile = Sketch1.Profiles.AddForSolid

    ' NEED TO USE THE THICKNESS PARAMETER TO DRIVE THE EXTRUSION THICKNESS HERE - DIMENSION IS IN cm, NOT mm.
    Dim oExtrudeDef As ExtrudeDefinition = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, PartFeatureOperationEnum.kJoinOperation)
	Dim oThickness As Double = Parameter("Thickness") / 10
	Call oExtrudeDef.SetDistanceExtent("Thickness", PartFeatureExtentDirectionEnum.kNegativeExtentDirection)
'    Call oExtrudeDef.SetDistanceExtent(oThickness, PartFeatureExtentDirectionEnum.kNegativeExtentDirection) ' PartFeatureExtentDirectionEnum.kSymmetricExtentDirection)
    Dim oExtrude1 As ExtrudeFeature = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
	oExtrude1.SetEndOfPart(True)
	'Dim thick As UserParameter = oCompDef.Parameters("Thickness")
	
	oCompDef.SetEndOfPartToTopOrBottom(False)

' Extrusion 2
    ' Get the top face of the extrusion to use for creating the new sketch.
    Dim oFrontFace As Face = oExtrude1.StartFaces.Item(1)

    ' Create a new sketch on this face, but use the method that allows you to
    ' control the orientation and orgin of the new sketch.
    Dim Sketch2 As PlanarSketch = oCompDef.Sketches.AddWithOrientation(oFrontFace, oCompDef.WorkAxes.Item(1), True, True, oCompDef.WorkPoints(1))
	' Create the sketch point for the origin, if it doesn't already exist.
    
	For Each oSketchPoint As SketchPoint In Sketch2.SketchPoints
        If oSketchPoint.ReferencedEntity Is oOriginWP Then
            oOriginSketchPoint = oSketchPoint
            Exit For
        End If
    Next
	
	
	If oOriginSketchPoint Is Nothing Then
		messagebox.Show("Creating sketch origin")
        ' Project the origin point onto the sketch.
        oOriginSketchPoint = Sketch2.AddByProjectingEntity(oOriginWP)
    End If

    ' Determine where in sketch space the point (0.5,0.5,0) is.
    Dim oCorner As Point2d = Sketch2.ModelToSketchSpace(oTransGeom.CreatePoint((- Parameter("CutoutWidth") / 10) / 2, ((Parameter("Length") / 10) / 2) - (Parameter("CutoutDepth") / 10), 0)) ' Thickness))
	
	oRectangleLines = Nothing
	
    ' Create the interior 3cm x 2cm rectangle for the pocket.
    oRectangleLines = Sketch2.SketchLines.AddAsTwoPointRectangle(oCorner, oTransGeom.CreatePoint2d(oCorner.X + 1, oCorner.Y + 0.5))
	
	debugrectanglelines(oRectangleLines, StartTime)
	
	TextPoint = ThisApplication.TransientGeometry.CreatePoint2d(0, ((Parameter("CutoutWidth") /10)+ 1 +((Parameter("Length") / 10) / 2)) / 2)
	oConstraint = Sketch2.DimensionConstraints.AddTwoPointDistance(oRectangleLines(1).EndSketchPoint, oRectangleLines(3).EndSketchPoint, _
																	DimensionOrientationEnum.kHorizontalDim, TextPoint)
	oConstraint.Parameter.Expression = "CutoutWidth"
	TextPoint = ThisApplication.TransientGeometry.CreatePoint2d(- ((Parameter("Length") / 10) / 2) - 10, ((Parameter("Length") / 10) / 2) + (Parameter("CutoutDepth") /10))
	oConstraint = Sketch2.DimensionConstraints.AddTwoPointDistance(oRectangleLines(2).EndSketchPoint, oRectangleLines(4).EndSketchPoint, _
																	DimensionOrientationEnum.kVerticalDim, TextPoint)
	oConstraint.Parameter.Expression = "CutoutDepth"
	TextPoint = ThisApplication.TransientGeometry.CreatePoint2d(- ((Parameter("CutoutWidth") / 10) / 2) - 10, (((Parameter("Length") / 10) / 2) - (Parameter("CutoutDepth") /10)) / 2)
	Sketch2.DimensionConstraints.AddTwoPointDistance(oRectangleLines(2).StartSketchPoint, oOriginSketchPoint, DimensionOrientationEnum.kVerticalDim, TextPoint)

	TextPoint = ThisApplication.TransientGeometry.CreatePoint2d(0, ((((Parameter("Length") / 10) / 2) - (Parameter("CutoutDepth") /10)) / 2) - 1)
	oConstraint = Sketch2.DimensionConstraints.AddTwoPointDistance(oOriginSketchPoint, oRectangleLines(1).StartSketchPoint, DimensionOrientationEnum.kHorizontalDim, TextPoint)

    ' Create a profile.
    oProfile = Sketch2.Profiles.AddForSolid

    ' Create a pocket .25 cm deep (using distance extent).
    Dim oExtrudeDef2 As ExtrudeDefinition = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, PartFeatureOperationEnum.kCutOperation)
    'Call oExtrudeDef.SetDistanceExtent(oThickness, PartFeatureExtentDirectionEnum.kNegativeExtentDirection)
	Call oExtrudeDef2.SetThroughAllExtent(PartFeatureExtentDirectionEnum.kNegativeExtentDirection)
'	
    Dim oExtrude2 As ExtrudeFeature = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef2)
'	oExtrude2.SetEndofPart(True)
'	oExtrude2.
'	oCompDef.SetEndOfPartToTopOrBottom(False)
'CIRCULAR PATTERN
	Dim objColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	objColl.Add(oExtrude2)

	Dim oRotation As String = PatternRotation '360 * 0.0174533
	'MessageBox.Show(oRotation)
	Dim circularPatternDef As CircularPatternFeatureDefinition = oCompDef.Features.CircularPatternFeatures.CreateDefinition( _
																objColl, oCompDef.WorkAxes("Z Axis"), False, 4, "PatternRotation") 'oRotation)
	Parameter.UpdateAfterChange = True
	Dim circularPat As CircularPatternFeature = oCompDef.Features.CircularPatternFeatures.AddByDefinition(circularPatternDef)
	circularPat.SetEndOfPart(True)
'	circularPat.Definition.Angle = "PatternRotation"
	'THIS IS A KLUDGE TO FIX AUTODESK'S BUG AND SHOULDN'T BE NECESSARY!:
	'ALSO NOW NEED TO FIGURE OUT THE HIGHEST PARAMETER NUMBER AND USE THAT INSTEAD OF THE PREVIOUS d142!
	Dim ModelParamList As List(Of ModelParameter) = New List(Of ModelParameter)
	For Each MParameter As ModelParameter In oCompDef.Parameters.ModelParameters
		ModelParamList.Add(MParameter)	
	Next
	
	'debug
	MessageBox.Show(ModelParamList.Count)
	Dim maxParam As Parameter = Nothing
	Dim maxParamNum As Integer = 0
	If ModelParamList.Count > 0 Then
		ModelParamList.Sort(Function(x As ModelParameter, y As ModelParameter) x.Name.CompareTo(y.Name))
		'DEBUG:
'		For Each mParam As ModelParameter In ModelParamList
'			MessageBox.Show(mParam.Name)
'		Next
		maxParam = (From param As ModelParameter In ModelParamList Select param).Last()
	End If
	Dim p As ModelParameter = maxParam 'oCompDef.Parameters("d142") 'Parameter.Param("d142")
	p.Expression = "PatternRotation"
	oCompDef.SetEndOfPartToTopOrBottom(False)
	
'Centre Hole
	Dim holeDef As HolePlacementDefinition = oCompDef.Features.HoleFeatures.CreatePointPlacementDefinition(oOriginWP, oCompDef.WorkAxes(3))
	Dim ThruHole As HoleFeature = oCompDef.Features.HoleFeatures.AddDrilledByThroughAllExtent(holeDef, HoleDia / 10, PartFeatureExtentDirectionEnum.kPositiveExtentDirection)
	Dim bodiesColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	bodiesColl.Add(oCompDef.SurfaceBodies(1))
	ThruHole.SetAffectedBodies(bodiesColl)
	ThruHole.SetEndOfPart(True)
	ThruHole.HoleDiameter.Expression = "HoleDia"
	oCompDef.SetEndOfPartToTopOrBottom(False)
' NOT SURE WHAT THIS IS FOR..?
    ' Move the end of part above this extrude feature since the edit
    ' we are going to perform on this feature involves BREP input.
'    Call oExtrude2.SetEndOfPart(True)

'    ' Get the back face of the first extrusion to use as termination plane.
'    Dim oBackFace As Face = oExtrude1.EndFaces.Item(1)

'    ' Change the extent type of the feature.
'    Call oExtrude2.Definition.SetToExtent(oBackFace, False)

'    ' Move the end of part back to bottom of the feature tree.
'    Call oCompDef.SetEndOfPartToTopOrBottom(False)

'    ' The following edit of the feature does not involve BREP input.
'    ' Hence, no need to move the end of part.
'    Call oExtrude2.Definition.SetDistanceExtent(0.25, kNegativeExtentDirection)

iProperties.Value("Custom", "Vault Category") = Parameter("VaultCategory")
iProperties.Value("Custom", "MFG Code") = Parameter("MFGCode")
iProperties.Value("Custom", "Customer Name") = Parameter("CustomerName")


ThisApplication.ActiveView.Fit

'Opens iProperties dialogue
If Parameter("DisplayiPropertiesAfterFormFilled") Then
	Dim iPropertiesCtrldef As Inventor.ControlDefinition = ThisApplication.CommandManager.ControlDefinitions.Item("PartiPropertiesCmd")
	iPropertiesCtrldef.Execute()
End If

End Sub

Dim oOriginSketchPoint As SketchPoint

''' <summary>
''' only useful for debugging!
''' </summary>
''' <param name="oRectangleLines"></param>
''' <param name="StartTime"></param>
Public Sub debugrectanglelines(oRectangleLines As SketchEntitiesEnumerator, StartTime As DateTime)
	Dim ElapsedTime As TimeSpan
	Dim oLine As SketchLine
	
	For i As Integer = 1 To oRectangleLines.Count
		ElapsedTime = Now().Subtract(StartTime)
		ThisApplication.StatusBarText = oRectangleLines.Count
		
		'ThisApplication.StatusBarText = MessageBox.Show("Operation took " & ElapsedTime.TotalSeconds & " Seconds", "Graitec iLogic")
		oLine = oRectangleLines(i)
		If Parameter("HighlightSketchLines") Then
			Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(255, 0, 0) 'Red

			Dim originalcolour As Color = oLine.OverrideColor
			oLine.OverrideColor = oColor
			MessageBox.Show("Operation took " & ElapsedTime.TotalSeconds & " Seconds", "Graitec iLogic")
			oLine.OverrideColor = originalcolour
		End If
		
	Next	
End Sub

Public Sub Draw_Rectangle()
    ' Check to make sure a sketch is open.
    If Not TypeOf ThisApplication.ActiveEditObject Is PlanarSketch Then
        MsgBox ("A sketch must be active.")
        Exit Sub
    End If

    ' Set a reference to the active sketch.
    Dim oSketch As PlanarSketch = ThisApplication.ActiveEditObject

    ' Set a reference to the transient geometry collection.
    Dim oTransGeom As TransientGeometry = ThisApplication.TransientGeometry
     
    'Gets user input for size and converts english to metric (since the default input for making lines is metric)
    Dim oUOM As UnitsOfMeasure = ThisApplication.ActiveDocument.UnitsOfMeasure
    Dim x1, y1, x2, y2 As Double
	Dim x,y As Double
    x = CDbl(InputBox(Prompt:="X dimension.", Title:="X", DefaultResponse:="1"))
    y = CDbl(InputBox(Prompt:="Y dimension.", Title:="Y", DefaultResponse:="1"))
    x1 = oUOM.GetValueFromExpression(-x / 2, "in")
    y1 = oUOM.GetValueFromExpression(-y / 2, "in")
    x2 = oUOM.GetValueFromExpression(x / 2, "in")
    y2 = oUOM.GetValueFromExpression(y / 2, "in")
    
    'undo stuff
    Dim oTransMgr As TransactionManager = ThisApplication.TransactionManager
    Dim oTrans As Transaction  = oTransMgr.StartTransaction(ThisApplication.ActiveDocument, "undo rectangle")
    'undo stuff
    
    'Drawing the lines
    'Dim oLines(1 To 4) As SketchLine
    Dim oLine1 As SketchLine = oSketch.SketchLines.AddByTwoPoints(oTransGeom.CreatePoint2d(x1, y1), oTransGeom.CreatePoint2d(x1, y2))
    Dim oLine2 As SketchLine = oSketch.SketchLines.AddByTwoPoints(oLine1.EndSketchPoint, oTransGeom.CreatePoint2d(x2, y2))
    Dim oLine3 As SketchLine = oSketch.SketchLines.AddByTwoPoints(oLine2.EndSketchPoint, oTransGeom.CreatePoint2d(x2, y1))
    Dim oLine4 As SketchLine = oSketch.SketchLines.AddByTwoPoints(oLine3.EndSketchPoint, oLine1.StartSketchPoint)

    'Setting up the constraints for the size of the rectangle
    Dim oConstraint As TwoPointDistanceDimConstraint
    Dim TextPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(x1 - 1, y1)
    oConstraint = oSketch.DimensionConstraints.AddTwoPointDistance(oLine1.StartSketchPoint, oLine1.EndSketchPoint, DimensionOrientationEnum.kVerticalDim, TextPoint, False)
    TextPoint = ThisApplication.TransientGeometry.CreatePoint2d(x1, y2 + 1)
    oConstraint = oSketch.DimensionConstraints.AddTwoPointDistance(oLine2.StartSketchPoint, oLine2.EndSketchPoint, DimensionOrientationEnum.kHorizontalDim, TextPoint, False)

    'subroutine to make point on origin (couldn't find a way to just reference origin point)
    Call Origin
    
    'Sets up getting parameters, so contraints from origin to rectangle can be turned into functions of the side lengths to keep the rectangle centered
    ' Get the Parameters object. Assumes a part or assembly document is active.
    Dim oParameters As Parameters = ThisApplication.ActiveDocument.ComponentDefinition.Parameters
    
    TextPoint = ThisApplication.TransientGeometry.CreatePoint2d(x1 / 2, y2 / 2)
    oConstraint = oSketch.DimensionConstraints.AddTwoPointDistance(oLine1.EndSketchPoint, oOriginSketchPoint, DimensionOrientationEnum.kVerticalDim, TextPoint, False)

    TextPoint = ThisApplication.TransientGeometry.CreatePoint2d(x2 / 2, y2 / 2)
    oConstraint = oSketch.DimensionConstraints.AddTwoPointDistance(oLine2.EndSketchPoint, oOriginSketchPoint, DimensionOrientationEnum.kHorizontalDim, TextPoint, False)
    
    ' Get the parameter.
    Dim oLengthParam As Parameter = oParameters.Item("d2")
    oLengthParam.Expression = "d0/2"
    oLengthParam = oParameters.Item("d3")
    oLengthParam.Expression = "d1/2"
    
    oTrans.End 'end undo
    
    ' Update the document.
    ThisApplication.ActiveDocument.Update
    'Zoom to fit
    ThisApplication.ActiveView.Fit

End Sub

Private Sub Origin()
    'Get the active part document.
    Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument

    Dim oPartDef As PartComponentDefinition = oPartDoc.ComponentDefinition

    ' Get an existing sketch.  This arbitrarily gets the first sketch.
    Dim oSketch As PlanarSketch = oPartDef.Sketches.Item(1)
   
    ' Check to see if there's a point in the sketch that represents the origin.
    Dim oOriginWP As WorkPoint  = oPartDef.WorkPoints.Item(1)
    'set as global above
    'Dim oOriginSketchPoint As SketchPoint
    Dim oSketchPoint As SketchPoint
    For Each oSketchPoint In oSketch.SketchPoints
        If oSketchPoint.ReferencedEntity Is oOriginWP Then
            oOriginSketchPoint = oSketchPoint
            Exit For
        End If
    Next
   
    ' Create the sketch point for the origin, if it doesn't already exist.
    If oOriginSketchPoint Is Nothing Then
        ' Project the origin point onto the sketch.
        oOriginSketchPoint = oSketch.AddByProjectingEntity(oPartDef.WorkPoints.Item(1))
    End If
   
End Sub

🙂

Message 3 of 8

Boopathi_Sivakumar
Collaborator
Collaborator

@AlexFielder 

thanks for your reply .

 

I couldn't use your codes it throws me lot of error. how should i use your code.

 

And moreover i am just a beginner and  more curious on what mistake i made on my code

 

Thanks,

Boopathi

Boopathi Sivakumar
Sr Application Engineer
www.usamcadsoft.in
Facebook | Twitter | LinkedIn

0 Likes
Message 4 of 8

AlexFielder
Advisor
Advisor

The sample I provided is iLogic (which runs inside of the Inventor UI). It might function in your application with some tweaks.

0 Likes
Message 5 of 8

JhoelForshav
Mentor
Mentor
Accepted solution

Try this as your button2 sub 🙂

I changed it so it uses points directly from previous lines to close the skecth. Add all lines to an objectcollection to use for the AddForSolid... Hopefully it will work for you

 

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

If Not TypeOf _InvApp.ActiveDocument Is PartDocument Then
MsgBox("Need to Create a Part")
MsgBox("Wish to create a Part", MsgBoxStyle.YesNo)

Odoc = _InvApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject, _InvApp.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject))
If Not TypeOf _InvApp.ActiveEditObject Is PlanarSketch Then
MsgBox("Need to Add a Sketch")
MsgBox("Wish to create a sketch", MsgBoxStyle.YesNo)

Opcd = _InvApp.ActiveDocument.ComponentDefinition
Oske = Opcd.Sketches.Add(Opcd.WorkPlanes.Item(3))
Otg = _InvApp.TransientGeometry

Dim Lin(3) As SketchLine
Dim HL As Decimal = (InputBox("Enter The Length of the Rectangle: ")) / 10
Dim VL As Decimal = (InputBox("Enter The Height of the Rectangle: ")) / 10


Dim oCollection As ObjectCollection = _InvApp.TransientObjects.CreateObjectCollection


Lin(0) = Oske.SketchLines.AddByTwoPoints(Otg.CreatePoint2d(0, 0), Otg.CreatePoint2d(HL, 0))
Lin(1) = Oske.SketchLines.AddByTwoPoints(Lin(0).EndSketchPoint, Otg.CreatePoint2d(HL, VL))
Lin(2) = Oske.SketchLines.AddByTwoPoints(Lin(1).EndSketchPoint, Otg.CreatePoint2d(0, VL))
Lin(3) = Oske.SketchLines.AddByTwoPoints(Lin(2).EndSketchPoint, Lin(0).StartSketchPoint)

Call Oske.GeometricConstraints.AddGround(Lin(0).StartSketchPoint)
Call Oske.GeometricConstraints.AddHorizontal(Lin(0))
Call Oske.GeometricConstraints.AddVertical(Lin(1))
Call Oske.GeometricConstraints.AddHorizontal(Lin(2))
Call Oske.GeometricConstraints.AddVertical(Lin(3))

Call Oske.DimensionConstraints.AddTwoPointDistance(Lin(0).StartSketchPoint, Lin(0).EndSketchPoint, DimensionOrientationEnum.kHorizontalDim, Otg.CreatePoint2d(0, -1))
Call Oske.DimensionConstraints.AddTwoPointDistance(Lin(1).StartSketchPoint, Lin(1).EndSketchPoint, DimensionOrientationEnum.kVerticalDim, Otg.CreatePoint2d(6, 0))
Call Oske.GeometricConstraints.AddEqualLength(Lin(1), Lin(3))
Call Oske.GeometricConstraints.AddEqualLength(Lin(0), Lin(2))

oCollection.Add(Lin(0))
oCollection.Add(Lin(1))
oCollection.Add(Lin(2))
oCollection.Add(Lin(3))


Dim pro1 As Profile
pro1 = Oske.Profiles.AddForSolid(False, oCollection)

End If
End If

 

End Sub

Message 6 of 8

Darkforce_the_ilogic_guy
Advisor
Advisor

how do I get your code to run on my computer .. I want to help even ... that I my be more newbie then you .. but how do I get it to run on my system so I can start testing stuff?

0 Likes
Message 7 of 8

Boopathi_Sivakumar
Collaborator
Collaborator

@Darkforce_the_ilogic_guy 

there are many ways to this,

1 . You can test your code in I-Logic environments also

2 . And in Inventor itself has the default VBA editor in the tools menu you can test your codes

123.PNG

3. External Visual Studio Application from there also you can work with the codes

 

Thanks,

Boopathi

Boopathi Sivakumar
Sr Application Engineer
www.usamcadsoft.in
Facebook | Twitter | LinkedIn

Message 8 of 8

Boopathi_Sivakumar
Collaborator
Collaborator
Accepted solution

@JhoelForshav 

Adding it to the collections, there is the trick it works.

 

And one more thing which went wrong with my code was instead of giving points for line directly, need to create a separate points and then assign those points  for lines will also solves this issue

 

like here is the code

  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        If Not TypeOf _InvApp.ActiveDocument Is PartDocument Then
            MsgBox("Need to Create a Part")
            MsgBox("Wish to create a Part", MsgBoxStyle.YesNo)

            Odoc = _InvApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject, _InvApp.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject))
            If Not TypeOf _InvApp.ActiveEditObject Is PlanarSketch Then
                MsgBox("Need to Add a Sketch")
                MsgBox("Wish to create a sketch", MsgBoxStyle.YesNo)

                Opcd = _InvApp.ActiveDocument.ComponentDefinition
                Oske = Opcd.Sketches.Add(Opcd.WorkPlanes.Item(3))
                Otg = _InvApp.TransientGeometry

                Dim Lin(3) As SketchLine
                Dim HL As Decimal = (InputBox("Enter The Length of the Rectangle: ")) / 10
                Dim VL As Decimal = (InputBox("Enter The Height of the Rectangle: ")) / 10

                Dim poi(3) As SketchPoint
                poi(0) = Otg.CreatePoint2d(0, 0)
                poi(1) = Otg.CreatePoint2d(HL, 0)
                poi(2) = Otg.CreatePoint2d(HL, VL)
                poi(3) = Otg.CreatePoint2d(0, VL)


                Lin(0) = Oske.SketchLines.AddByTwoPoints(poi(0), poi(1))
                Lin(1) = Oske.SketchLines.AddByTwoPoints(poi(1), poi(2))
                Lin(2) = Oske.SketchLines.AddByTwoPoints(poi(2), poi(3))
                Lin(3) = Oske.SketchLines.AddByTwoPoints(poi(3), poi(0))

                Call Oske.GeometricConstraints.AddGround(Lin(0).StartSketchPoint)
                Call Oske.GeometricConstraints.AddHorizontal(Lin(0))
                Call Oske.GeometricConstraints.AddVertical(Lin(1))
                Call Oske.GeometricConstraints.AddHorizontal(Lin(2))
                Call Oske.GeometricConstraints.AddVertical(Lin(3))

                Call Oske.DimensionConstraints.AddTwoPointDistance(Lin(0).StartSketchPoint, Lin(0).EndSketchPoint, DimensionOrientationEnum.kHorizontalDim, Otg.CreatePoint2d(0, -1))
                Call Oske.DimensionConstraints.AddTwoPointDistance(Lin(1).StartSketchPoint, Lin(1).EndSketchPoint, DimensionOrientationEnum.kVerticalDim, Otg.CreatePoint2d(6, 0))
                Call Oske.GeometricConstraints.AddEqualLength(Lin(1), Lin(3))
                Call Oske.GeometricConstraints.AddEqualLength(Lin(0), Lin(2))


                Dim pro1 As Profile
                pro1 = Oske.Profiles.AddForSolid

            End If
        End If

    End Sub

Thanks,

Boopathi

Boopathi Sivakumar
Sr Application Engineer
www.usamcadsoft.in
Facebook | Twitter | LinkedIn

0 Likes