Additional Class Materials SD125452 Having Fun Using Components Occurrences Matrices and Proxies With Inventor Softwares API Brian Ekins 1 PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Option Explicit

Public Sub PlaceWheels()


Dim wheelFile As String
wheelFile = "C:\Users\ekinsb\Documents\Presentations\AU 2017\Car Assembly\Wheel.ipt"
Dim asmDoc As AssemblyDocument
Set asmDoc = ThisApplication.ActiveDocument
Dim asmDef As AssemblyComponentDefinition
Set asmDef = asmDoc.ComponentDefinition

' Have a cylindrical axle face selected.


Dim axleFace As Face
Set axleFace = ThisApplication.CommandManager.Pick(kPartFaceCylindricalFilter, _
"Select the axle cylinder")

' Find the edges of the selected cylinder.


Dim edge1 As edge
Dim edge2 As edge
Set edge1 = axleFace.edges.Item(1)
Set edge2 = axleFace.edges.Item(2)

' Insert the wheel part


Dim wheelOccOne As ComponentOccurrence
Set wheelOccOne = asmDef.Occurrences.Add(wheelFile, ThisApplication.TransientGeometry.CreateMatrix)

' Find the edge in the wheel with the attribute.


Dim wheelDoc As PartDocument
Set wheelDoc = wheelOccOne.Definition.Document
Dim objs As ObjectCollection
Set objs = wheelDoc.AttributeManager.FindObjects("InsertEdge", "*")
Dim nativeWheelEdge As edge
Set nativeWheelEdge = objs.Item(1)

' Create a proxy for the wheel edge with respect to the just inserted occurrence.
Dim wheelEdgeProxyOne As edge
Call wheelOccOne.CreateGeometryProxy(nativeWheelEdge, wheelEdgeProxyOne)

' Create an insert constraint between the axle and wheel.


Call asmDef.Constraints.AddInsertConstraint(edge1, wheelEdgeProxyOne, False, 0)

' Create another occurrence using the existing wheel component.


Dim wheelOccTwo As ComponentOccurrence
Set wheelOccTwo = asmDef.Occurrences.AddByComponentDefinition(wheelOccOne.Definition,
ThisApplication.TransientGeometry.CreateMatrix)

' Create a proxy to the wheel edge for this occurrence.


Dim wheelEdgeProxyTwo As edge
Call wheelOccTwo.CreateGeometryProxy(nativeWheelEdge, wheelEdgeProxyTwo)

' Create an insert constraint between the axle and wheel.


Call asmDef.Constraints.AddInsertConstraint(edge2, wheelEdgeProxyTwo, False, 0)
End Sub
Public Sub GetCylinder()
' Have a cylindrical face selected.
Dim cylFace As Face
Set cylFace = ThisApplication.CommandManager.Pick(kPartFaceCylindricalFilter, _
"Select a cylinder")

' Get the two circular edges of the selected face.


Dim edge1 As edge
Dim edge2 As edge
Set edge1 = cylFace.edges.Item(1)
Set edge2 = cylFace.edges.Item(2)

' Get the center point of the two circular edges.


Dim center1 As Point
Dim center2 As Point
Set center1 = edge1.Geometry.center
Set center2 = edge2.Geometry.center

' Display the center point coordinates.


MsgBox "Cylinder points: (" & Format(center1.x, "0.000") & ", " & _
Format(center1.y, "0.000") & ", " & _
Format(center1.z, "0.000") & ") (" & _
Format(center2.x, "0.000") & ", " & _
Format(center2.y, "0.000") & ", " & _
Format(center2.z, "0.000") & ")"
End Sub
Public Sub Animate()
' Get the selected occurrence.
Dim occ As ComponentOccurrence
Set occ = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select an occurrence")

Dim tg As TransientGeometry
Set tg = ThisApplication.TransientGeometry

Dim pi As Double
pi = Atn(1) * 4

Dim z As Vector
Set z = tg.CreateVector(0, 0, 1)

' Create the point that defines the origin.


Dim pstn As Point
Set pstn = tg.CreatePoint(occ.Transformation.Translation.x, _
occ.Transformation.Translation.y, _
occ.Transformation.Translation.z)

' Create the vectors for the X, Y axes.


Dim steps As Integer
steps = 500
Dim i As Integer
For i = 1 To steps
Dim angle As Double
angle = ((pi * 2) / steps) * i

Dim x As Vector
Set x = tg.CreateVector(Cos(angle), Sin(angle), 0)

angle = angle + (pi * 0.5)


Dim y As Vector
Set y = tg.CreateVector(Cos(angle), Sin(angle), 0)

' Create the matrix and define it as a coordinate system.


Dim trans As Matrix
Set trans = tg.CreateMatrix
Call trans.SetCoordinateSystem(pstn, x, y, z)

' Set the transform of the occurrence.


' occ.Transformation = trans
Call occ.SetTransformWithoutConstraints(trans)
ThisApplication.ActiveView.Update
Next
End Sub
Public Sub Reposition()
' Get the selected occurrence.
Dim occ As ComponentOccurrence
Set occ = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select an occurrence")

Dim tg As TransientGeometry
Set tg = ThisApplication.TransientGeometry

Dim pi As Double
pi = Atn(1) * 4

' Create the vectors for the X, Y, and Z axes.


Dim angle As Double
angle = pi * 0.25
Dim x As Vector
Set x = tg.CreateVector(Cos(angle), Sin(angle), 0)

angle = pi * 0.75
Dim y As Vector
Set y = tg.CreateVector(Cos(angle), Sin(angle), 0)

Dim z As Vector
Set z = tg.CreateVector(0, 0, 1)

' Create the point that defines the origin.


Dim pstn As Point
Set pstn = tg.CreatePoint(3, 5, 0)

' Create the matrix and define it as a coordinate system.


Dim trans As Matrix
Set trans = tg.CreateMatrix
Call trans.SetCoordinateSystem(pstn, x, y, z)

' Set the transform of the occurrence.


occ.Transformation = trans
End Sub
Private Sub PartComponentReport()
' Get the component from the active document.
Dim partDoc As PartDocument
Set partDoc = ThisApplication.ActiveDocument
Dim partDef As PartComponentDefinition
Set partDef = partDoc.ComponentDefinition

' Report on the contents of the component.


Dim results As String
results = "Feature count: " & partDef.features.count & vbCrLf
results = results + "Work plane count: " & partDef.WorkPlanes.count & vbCrLf
results = results + "Work axis count: " & partDef.WorkAxes.count & vbCrLf
results = results + "Work point count: " & partDef.WorkPoints.count & vbCrLf
results = results + "2D Sketch count: " & partDef.sketches.count & vbCrLf
results = results + "3D Sketch count: " & partDef.Sketches3D.count & vbCrLf
results = results + "Body count: " & partDef.SurfaceBodies.count & vbCrLf
results = results + "Parameter count: " & partDef.Parameters.count & vbCrLf

Call MsgBox(results, , "Component Totals")


End Sub
Private Sub AssemblyComponentReport()
' Get the component from the active document.
Dim asmDoc As AssemblyDocument
Set asmDoc = ThisApplication.ActiveDocument
Dim asmDef As AssemblyComponentDefinition
Set asmDef = asmDoc.ComponentDefinition

' Report on the contents of the component.


Dim results As String
results = "Occurrence count: " & asmDef.Occurrences.count & vbCrLf
results = results + "Work plane count: " & asmDef.WorkPlanes.count & vbCrLf
results = results + "Work axis count: " & asmDef.WorkAxes.count & vbCrLf
results = results + "Work point count: " & asmDef.WorkPoints.count & vbCrLf
results = results + "2D Sketch count: " & asmDef.sketches.count & vbCrLf
results = results + "Constraint count: " & asmDef.Constraints.count & vbCrLf
results = results + "Joint count: " & asmDef.Joints.count & vbCrLf
results = results + "Parameter count: " & asmDef.Parameters.count & vbCrLf
results = results + "BOM Views count: " & asmDef.BOM.BOMViews.count & vbCrLf
results = results + "iMateDefinitions count: " & asmDef.iMateDefinitions.count & vbCrLf
results = results + "iMateResults count: " & asmDef.iMateResults.count & vbCrLf

Call MsgBox(results, , "Component Totals")


End Sub

You might also like