Create constraint on two faces

Create constraint on two faces

Anonymous
Not applicable
563 Views
1 Reply
Message 1 of 2

Create constraint on two faces

Anonymous
Not applicable

Hello everyone.

I'm new to Inventor API VBA.

I am trying to create two constraints on two details in an assembly.
I created a constraint on cylindrical faces.
Similarly, I created a constraint on two flat faces, but the wrong flat face is selected on the first detail.
I need to select a flat face inside the cut of the first part, but I do not understand how to do this.
Please help.
What am I doing wrong?

 

Assembly.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

My code:

Imports System.Runtime.InteropServices

Public Class Form1
    Dim oApp As Inventor.Application = GetInventorApplication()

    ''FUNCTION OF GETTING A REFERENCE TO Inventor
    Function GetInventorApplication() As Inventor.Application
        Dim InvApp As Inventor.Application
        Try
            InvApp = CType(Marshal.GetActiveObject("Inventor.Application"), Inventor.Application)
        Catch ex As Exception
            Dim InvType As Type = Type.GetTypeFromProgID("Inventor.Application")
            InvApp = CType(Activator.CreateInstance(InvType), Inventor.Application)
            InvApp.Visible = True
        End Try
        Return InvApp
    End Function

    Private Sub Create_Assembly(sender As Object, e As EventArgs) Handles Button1.Click
        Call Create_Assembly_Procedure()
    End Sub

    Sub Create_Assembly_Procedure()
        ''CREATION OF ASSEMBLY DOCUMENT
        Dim oAsDoc As Inventor.AssemblyDocument = oApp.Documents.Add(Inventor.DocumentTypeEnum.kAssemblyDocumentObject, oApp.GetTemplateFile(Inventor.DocumentTypeEnum.kAssemblyDocumentObject))

        ''OBTAINING OBJECT COMPONENTS
        Dim oAsCompDef As Inventor.AssemblyComponentDefinition = oApp.ActiveDocument.ComponentDefinition

        ''INDICATION OF THE MATRIX
        Dim oPositionMatrix As Inventor.Matrix = oApp.TransientGeometry.CreateMatrix

        ''CREATING A PARTS INLET
        Dim sFileNameDetail_1 As String = "D:Details\Detail_1.ipt"
        Dim sFileNameDetail_2 As String = "D:Details\Detail_2.ipt"
        Dim sFileNameDetail_3 As String = "D:Details\Detail_3.ipt"
        ''PARAMETERS OF PARTS POSITION
        Dim VectorDetail_2 As Inventor.Vector = oApp.TransientGeometry.CreateVector(3, 3, -5)
        Dim VectorDetail_3 As Inventor.Vector = oApp.TransientGeometry.CreateVector(3, 3, 10)
        ''PARTS INPUT
        Dim Detail_1 As Inventor.ComponentOccurrence = oAsDoc.ComponentDefinition.Occurrences.Add(sFileNameDetail_1, oPositionMatrix)
        oPositionMatrix.SetTranslation(VectorDetail_2)
        Dim Detail_2 As Inventor.ComponentOccurrence = oAsDoc.ComponentDefinition.Occurrences.Add(sFileNameDetail_2, oPositionMatrix)
        oPositionMatrix.SetTranslation(VectorDetail_3)
        Dim Detail_3 As Inventor.ComponentOccurrence = oAsDoc.ComponentDefinition.Occurrences.Add(sFileNameDetail_3, oPositionMatrix)

        ''SETTING THE CAMERA VIEW
        Dim oCamera As Inventor.Camera = oApp.ActiveView.Camera
        oCamera.ViewOrientationType = Inventor.ViewOrientationTypeEnum.kIsoTopRightViewOrientation
        oCamera.Apply()
        oApp.ActiveView.Fit()

        ''CONSTRAINT ON CYLINDRICAL FACES
        Dim oAxisDetail1 As Inventor.Face
        Dim oAxisDetail2 As Inventor.Face
        Dim oFace As Inventor.Face
        For Each oFace In Detail_1.SurfaceBodies(1).Faces
            If oFace.SurfaceType = Inventor.SurfaceTypeEnum.kCylinderSurface Then
                oAxisDetail1 = oFace
            End If
        Next
        For Each oFace In Detail_2.SurfaceBodies(1).Faces
            If oFace.SurfaceType = Inventor.SurfaceTypeEnum.kCylinderSurface Then
                oAxisDetail2 = oFace
            End If
        Next
        Dim oConstr As Inventor.AssemblyConstraint
        oConstr = oAsCompDef.Constraints.AddMateConstraint(oAxisDetail1, oAxisDetail2, 0,
                                                           Inventor.InferredTypeEnum.kInferredLine,
                                                           Inventor.InferredTypeEnum.kInferredLine)

        ''CONSTRAINT ON FLAT FACES
        Dim oFaceDetail1 As Inventor.Face
        Dim oFaceDetail2 As Inventor.Face
        For Each oFace In Detail_1.SurfaceBodies(1).Faces
            If oFace.SurfaceType = Inventor.SurfaceTypeEnum.kPlaneSurface Then
                oFaceDetail1 = oFace
            End If
        Next
        For Each oFace In Detail_2.SurfaceBodies(1).Faces
            If oFace.SurfaceType = Inventor.SurfaceTypeEnum.kPlaneSurface Then
                oFaceDetail2 = oFace
            End If
        Next
        oConstr = oAsCompDef.Constraints.AddMateConstraint(oFaceDetail1, oFaceDetail2, 0,
                                                           Inventor.InferredTypeEnum.kNoInference,
                                                           Inventor.InferredTypeEnum.kNoInference)
    End Sub
End Class

 

0 Likes
564 Views
1 Reply
Reply (1)
Message 2 of 2

FINET_Laurent
Advisor
Advisor

Hi,

 

Instrad of iterating through all faces of the occurrence you could specifiy the face number like so :

 

Dim oFaceDetail1 As Inventor.Face = Detail_1.SurfaceBodies(1).Faces.Item(x)

x is the face item number, to get it you could use this rule in the part document : 

Sub Main

Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

If oDoc.DocumentType <> kPartDocumentObject Then
	
	MessageBox.Show("This functionality is only aviable in a part document!", "Error! :C", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	Exit Sub
	
End If

Dim oPartCompDef As PartComponentDefinition
oPartCompDef = oDoc.ComponentDefinition

Dim Face As Face
Dim oFace1 As Face = ThisApplication.CommandManager.Pick(kPartFaceFilter, "Pick Face")

If oFace1 Is Nothing Then 
	
	Exit Sub
	
End If 
	
Dim oSurfaceBody As SurfaceBody = oFace1.Parent
Dim i As Integer 

Try 
	
	For i = 1 To 100000
	
		If oFace1.InternalName = oSurfaceBody.Faces.Item(i).InternalName Then
		
			MessageBox.Show("    " & i, "Face internal item number : ", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)

				Exit Sub
			
		End If
	
	Next

Catch
	
	MessageBox.Show("Unexpected error occurred!", "Error! :C", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	Exit Sub
		
End Try

End Sub

Hope this helps,


Regards,

 

FINET L.

 

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes