Assembly Place Component Preview

Assembly Place Component Preview

mat_hijs
Collaborator Collaborator
149 Views
0 Replies
Message 1 of 1

Assembly Place Component Preview

mat_hijs
Collaborator
Collaborator

I'm trying to create a tool that will allow me to select a file and specify the X, Y and Z coordinate., the X, Y and Z rotation and the name of a work point in that model. It should then place the component, rotate it as necessary and then move the selected work point to the specified coordinates. I think I got it to work properly, but I would like to have a preview before actually placing the component.

I honestly have no idea where to start on this, so any help would be appreciated. I would like the preview to update dynamically as the coordinates, rotations or insertion point are changed.

 

This is my code so far:

AddReference "MSInventorTools.dll"
Sub Main
If ThisApplication.UserInterfaceManager.ActiveEnvironment IsNot ThisApplication.UserInterfaceManager.Environments.Item("AMxAssemblyEnvironment") Then
	' Show an Error Message
	MessageBox.Show("This command can only be ran in the assembly environment", iLogicVb.RuleName, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
	Exit Sub
End If

' Create a MiniToolbar
Dim oMiniToolbar As MiniToolbar = ThisApplication.CommandManager.CreateMiniToolbar
	oMiniToolbar.ShowOK = True
	oMiniToolbar.ShowApply = True
	oMiniToolbar.ShowCancel = True
	oMiniToolbar.ShowOptionBox = False
	oMiniToolbar.ShowHandle = True

' Set a reference to the MiniToolbar Controls
Dim oMiniToolbarControls As MiniToolbarControls = oMiniToolbar.Controls

' Add a MiniToolbar Control to select a File
Dim oButton_SelectFile As MiniToolbarButton = oMiniToolbarControls.AddButton("Button_SelectFile", "Select File", "Select a file to place the component.", , )

' Add a MiniToolbar Control for the File Path
Dim oLabel_FilePath As MiniToolbarControl = oMiniToolbarControls.AddLabel("Label_FilePath", "", "", , )

' Add MiniToolbar Controls for the X, Y and Z Coordinates
Dim oNewLine_Coordinates As MiniToolbarControl = oMiniToolbarControls.AddNewLine

Dim oLabel_Coordinates As MiniToolbarControl = oMiniToolbarControls.AddLabel("Label_Coordinates", "Coordinates", "Enter the X, Y and Z coordinates.", , )

Dim oValueEditor_CoordinateX As MiniToolbarValueEditor = oMiniToolbarControls.AddValueEditor("ValueEditor_CoordinateX", "Enter the X coordinate", ValueUnitsTypeEnum.kLengthUnits, 0, "X", 0, , )
	oValueEditor_CoordinateX.AllowMeasure = False
	oValueEditor_CoordinateX.AllowShowDimensions = False
	
Dim oValueEditor_CoordinateY As MiniToolbarValueEditor = oMiniToolbarControls.AddValueEditor("ValueEditor_CoordinateY", "Enter the Y coordinate", ValueUnitsTypeEnum.kLengthUnits, 0, "Y", 0, , )
	oValueEditor_CoordinateY.AllowMeasure = False
	oValueEditor_CoordinateY.AllowShowDimensions = False
	
Dim oValueEditor_CoordinateZ As MiniToolbarValueEditor = oMiniToolbarControls.AddValueEditor("ValueEditor_CoordinateZ", "Enter the Z coordinate", ValueUnitsTypeEnum.kLengthUnits, 0, "Z", 0, , )
	oValueEditor_CoordinateZ.AllowMeasure = False
	oValueEditor_CoordinateZ.AllowShowDimensions = False

' Add MiniToolbar Controls for the X, Y and Z Rotations
Dim oNewLine_Rotations As MiniToolbarControl = oMiniToolbarControls.AddNewLine

Dim oLabel_Rotations As MiniToolbarControl = oMiniToolbarControls.AddLabel("Label_Rotations", "Rotations", "Enter the X, Y and Z Rotations.", , )

Dim ValueEditor_RotationX As MiniToolbarValueEditor = oMiniToolbarControls.AddValueEditor("ValueEditor_RotationX", "Enter the X Rotation", ValueUnitsTypeEnum.kAngleUnits, 0, "X", 0, , )
	ValueEditor_RotationX.AllowMeasure = False
	ValueEditor_RotationX.AllowShowDimensions = False
	
Dim oValueEditor_RotationY As MiniToolbarValueEditor = oMiniToolbarControls.AddValueEditor("ValueEditor_RotationY", "Enter the Y Rotation", ValueUnitsTypeEnum.kAngleUnits, 0, "Y", 0, , )
	oValueEditor_RotationY.AllowMeasure = False
	oValueEditor_RotationY.AllowShowDimensions = False
	
Dim oValueEditor_RotationZ As MiniToolbarValueEditor = oMiniToolbarControls.AddValueEditor("ValueEditor_RotationZ", "Enter the Z Rotation", ValueUnitsTypeEnum.kAngleUnits, 0, "Z", 0, , )
	oValueEditor_RotationZ.AllowMeasure = False
	oValueEditor_RotationZ.AllowShowDimensions = False

' Add MiniToolbar Controls for the Options
Dim oNewLine_Options As MiniToolbarControl = oMiniToolbarControls.AddNewLine

Dim oLabel_SelectInsertionPoint As MiniToolbarControl = oMiniToolbarControls.AddLabel("Label_SelectInsertionPoint", "Select Insertion Point", "Select the workpoint that should be placed on the specified coordinates.", , )

Dim oComboBox_SelectInsertionPoint As MiniToolbarComboBox = oMiniToolbarControls.AddComboBox("Combo_SelectInsertionPoint", True, True, 200)

' Disable the MiniToolbar Controls until a File is selected
EnableControls(oMiniToolbar, False)

' Show the MiniToolbar
oMiniToolbar.Visible = True

' Set a Reference to the MiniToolbar Events
Dim oMiniToolbarEvents As New MiniToolbarEvents(ThisApplication, oMiniToolbar)
End Sub

Public Shared Sub EnableControls(ByVal oMiniToolbar As MiniToolbar, ByVal bEnabled As Boolean)
	' Set a reference to the MiniToolbar Controls
	Dim oMiniToolbarControls As MiniToolbarControls = oMiniToolbar.Controls
	
	' Enable or Disable the MiniToolbar Controls
	oMiniToolbar.EnableOK = bEnabled
	oMiniToolbar.EnableApply = bEnabled
	
	Dim oLabel_FilePath As MiniToolbarControl = oMiniToolbarControls.Item("Label_FilePath")
		oLabel_FilePath.Visible = bEnabled
	
	Dim oLabel_Coordinates As MiniToolbarControl = oMiniToolbarControls.Item("Label_Coordinates")
		oLabel_Coordinates.Visible = bEnabled
	
	Dim oValueEditor_CoordinateX As MiniToolbarValueEditor = oMiniToolbarControls.Item("ValueEditor_CoordinateX")
		oValueEditor_CoordinateX.Visible = bEnabled
		
	Dim oValueEditor_CoordinateY As MiniToolbarValueEditor = oMiniToolbarControls.Item("ValueEditor_CoordinateY")
		oValueEditor_CoordinateY.Visible = bEnabled
		
	Dim oValueEditor_CoordinateZ As MiniToolbarValueEditor = oMiniToolbarControls.Item("ValueEditor_CoordinateZ")
		oValueEditor_CoordinateZ.Visible = bEnabled
	
	Dim oLabel_Rotations As MiniToolbarControl = oMiniToolbarControls.Item("Label_Rotations")
		oLabel_Rotations.Visible = bEnabled
	
	Dim ValueEditor_RotationX As MiniToolbarValueEditor = oMiniToolbarControls.Item("ValueEditor_RotationX")
		ValueEditor_RotationX.Visible = bEnabled
		
	Dim oValueEditor_RotationY As MiniToolbarValueEditor = oMiniToolbarControls.Item("ValueEditor_RotationY")
		oValueEditor_RotationY.Visible = bEnabled
		
	Dim oValueEditor_RotationZ As MiniToolbarValueEditor = oMiniToolbarControls.Item("ValueEditor_RotationZ")
		oValueEditor_RotationZ.Visible = bEnabled
	
	Dim oLabel_SelectInsertionPoint As MiniToolbarControl = oMiniToolbarControls.Item("Label_SelectInsertionPoint")
		oLabel_SelectInsertionPoint.Visible = bEnabled
	
	Dim oComboBox_SelectInsertionPoint As MiniToolbarComboBox = oMiniToolbarControls.Item("Combo_SelectInsertionPoint")
		oComboBox_SelectInsertionPoint.Visible = bEnabled
End Sub

Class MiniToolbarEvents
	Private ThisApplication As Inventor.Application
	Private WithEvents MiniToolbar As MiniToolbar
	Private WithEvents Button_SelectFile As MiniToolbarButton
	Private Label_FilePath As MiniToolbarControl
	Private ValueEditor_CoordinateX As MiniToolbarValueEditor
	Private ValueEditor_CoordinateY As MiniToolbarValueEditor
	Private ValueEditor_CoordinateZ As MiniToolbarValueEditor
	Private ValueEditorRotationX As MiniToolbarValueEditor
	Private ValueEditor_RotationY As MiniToolbarValueEditor
	Private ValueEditor_RotationZ As MiniToolbarValueEditor
	Private Label_SelectInsertionPoint As MiniToolbarControl
	Private ComboBox_SelectInsertionPoint As MiniToolbarComboBox
	Private FilePath As String = ""
	
	Public Sub New(ByVal oApplication As Inventor.Application, ByVal oMiniToolbar As MiniToolbar)
		ThisApplication = oApplication
		MiniToolbar = oMiniToolbar
	    Button_SelectFile = MiniToolbar.Controls.Item("Button_SelectFile")
		Label_FilePath = MiniToolbar.Controls.Item("Label_FilePath")
		ValueEditor_CoordinateX = MiniToolbar.Controls.Item("ValueEditor_CoordinateX")
		ValueEditor_CoordinateY = MiniToolbar.Controls.Item("ValueEditor_CoordinateY")
		ValueEditor_CoordinateZ = MiniToolbar.Controls.Item("ValueEditor_CoordinateZ")
		ValueEditorRotationX = MiniToolbar.Controls.Item("ValueEditor_RotationX")
		ValueEditor_RotationY = MiniToolbar.Controls.Item("ValueEditor_RotationY")
		ValueEditor_RotationZ = MiniToolbar.Controls.Item("ValueEditor_RotationZ")
		Label_SelectInsertionPoint = MiniToolbar.Controls.Item("Label_SelectInsertionPoint")
		ComboBox_SelectInsertionPoint = MiniToolbar.Controls.Item("Combo_SelectInsertionPoint")
	End Sub
	
	Private Sub Button_SelectFile_OnClick() Handles Button_SelectFile.OnClick
		' Select a Model File
		FilePath = MSInventorTools.MS_OpenFileDialogs.OpenModelFilesDialog(False)(0)
		
		' Enable or Disable the MiniToolbar Controls
		If String.IsNullOrWhiteSpace(FilePath) = False Then
			Label_FilePath.DisplayName = FilePath
			ThisRule.EnableControls(MiniToolbar, True)
			GetWorkPointsFromComponent()
		Else
			Label_FilePath.DisplayName = ""
			ComboBox_SelectInsertionPoint.Clear
			ThisRule.EnableControls(MiniToolbar, False)
		End If
	End Sub
	
	Private Sub MiniToolbar_OnOK() Handles MiniToolbar.OnOK 
		PlaceComponent()
	End Sub
	
	Private Sub MiniToolbar_OnApply() Handles MiniToolbar.OnApply
		PlaceComponent()
	End Sub
	
	Private Sub MiniToolbar_OnCancel() Handles MiniToolbar.OnCancel
	End Sub
	
	Private Sub GetWorkPointsFromComponent()
		' Open the Document
		Dim oDoc As Document = ThisApplication.Documents.Open(FilePath, False)
		
		' Set a reference to the Component Definition
		Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition
		
		' Set a reference to the Work Points Collection
		Dim oWorkPoints As WorkPoints = oCompDef.WorkPoints
		
		' Clear the Combo Box
		ComboBox_SelectInsertionPoint.Clear
		
		' Populate the Combo Box
		For i As Integer = 1 To oWorkPoints.Count
			ComboBox_SelectInsertionPoint.AddItem(oWorkPoints.Item(i).Name, "")
		Next
		
		' Close the Document
		oDoc.Close(True)
	End Sub
	
	Private Sub PlaceComponent()
		' Check if the expressions are valid
		If ValueEditor_CoordinateX.IsExpressionValid = True AndAlso
				ValueEditor_CoordinateY.IsExpressionValid = True AndAlso
				ValueEditor_CoordinateZ.IsExpressionValid = True AndAlso
				ValueEditorRotationX.IsExpressionValid = True AndAlso
				ValueEditor_RotationY.IsExpressionValid = True AndAlso
				ValueEditor_RotationZ.IsExpressionValid = True Then
			
			' Set the Values
			Dim oUnitsOfMeasure As UnitsOfMeasure = ThisApplication.UnitsOfMeasure
			Dim LocationX As Double = oUnitsOfMeasure.ConvertUnits(ValueEditor_CoordinateX.Value, UnitsTypeEnum.kDatabaseLengthUnits, UnitsTypeEnum.kMillimeterLengthUnits)
			Dim LocationY As Double = oUnitsOfMeasure.ConvertUnits(ValueEditor_CoordinateY.Value, UnitsTypeEnum.kDatabaseLengthUnits, UnitsTypeEnum.kMillimeterLengthUnits)
			Dim LocationZ As Double = oUnitsOfMeasure.ConvertUnits(ValueEditor_CoordinateZ.Value, UnitsTypeEnum.kDatabaseLengthUnits, UnitsTypeEnum.kMillimeterLengthUnits)
			Dim RotationX As Double = oUnitsOfMeasure.ConvertUnits(ValueEditorRotationX.Value, UnitsTypeEnum.kDatabaseAngleUnits, UnitsTypeEnum.kDegreeAngleUnits)
			Dim RotationY As Double = oUnitsOfMeasure.ConvertUnits(ValueEditor_RotationY.Value, UnitsTypeEnum.kDatabaseAngleUnits, UnitsTypeEnum.kDegreeAngleUnits)
			Dim RotationZ As Double = oUnitsOfMeasure.ConvertUnits(ValueEditor_RotationZ.Value, UnitsTypeEnum.kDatabaseAngleUnits, UnitsTypeEnum.kDegreeAngleUnits)
			
			' Place the Component
			Dim oOcc As ComponentOccurrence = MSInventorTools.MS_Occurrence.PlaceOccurrence(FilePath, LocationX, LocationY, LocationZ, RotationX, RotationY, RotationZ, 0, 0, 0, 0, 0, 0, True)
			
			' Move the Occurrence to its Insertion Point
			MSInventorTools.MS_Occurrence.MoveOccurrenceToInsertionPoint(oOcc, ComboBox_SelectInsertionPoint.SelectedItem.Text)
		End If
	End Sub
End Class

 

This has some references to functions in my add-in. The code of these functions are here:

Public Shared Function PlaceOccurrence(ByVal sFilePath As String, ByVal Location_X As Double, ByVal Location_Y As Double, ByVal Location_Z As Double, ByVal Rotation_X As Double, ByVal Rotation_Y As Double, ByVal Rotation_Z As Double, ByVal Base_Location_X As Double, ByVal Base_Location_Y As Double, ByVal Base_Location_Z As Double, ByVal Base_Rotation_X As Double, ByVal Base_Rotation_Y As Double, ByVal Base_Rotation_Z As Double, Optional ByVal bGrounded As Boolean = True) As ComponentOccurrence
	Dim oResult As ComponentOccurrence

	' Set a reference to the Assembly Document
	Dim oAsmDoc As AssemblyDocument = g_inventorApplication.ActiveDocument

	' Set a reference to the Assembly Component Definition
	Dim oAsmCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition

	' Create a Matrix
	Dim oOccMatrix As Matrix = MS_Matrices.CreateMatrixFromLocationAndRotation(Location_X, Location_Y, Location_Z, Rotation_X, Rotation_Y, Rotation_Z)

	' Create a Matrix
	Dim oBaseMatrix As Matrix = MS_Matrices.CreateMatrixFromLocationAndRotation(Base_Location_X, Base_Location_Y, Base_Location_Z, Base_Rotation_X, Base_Rotation_Y, Base_Rotation_Z)

	' PreMultiply the Occurrence Matrix by the Base Matrix
	oOccMatrix.PreMultiplyBy(oBaseMatrix)

	' Set a reference to the Component Occurrences
	Dim oOccurrences As ComponentOccurrences = oAsmCompDef.Occurrences

	' Place a Component Occurrence
	Dim oOcc As ComponentOccurrence = oOccurrences.Add(sFilePath, oOccMatrix)

	' Ground the Component Occurrence
	oOcc.Grounded = bGrounded

	' Set the Default View Representation if the Component Occurrence is an Assembly
	If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then oOcc.SetDesignViewRepresentation("Default", , True)

	' Return the Component Occurrence
	oResult = oOcc

	Return oResult
End Function

Public Shared Function MoveOccurrenceToInsertionPoint(ByVal oOcc As ComponentOccurrence, ByVal sWorkPointName As String) As ComponentOccurrence
	Dim oResult As ComponentOccurrence

	' Set a reference to the Document of the Component Occurrence
	Dim oOccDoc As Document = oOcc.ReferencedDocumentDescriptor.ReferencedDocument

	' Get the Origin Point WorkPoint
	Dim oWPoint_Origin_Point As WorkPoint = oOccDoc.ComponentDefinition.WorkPoints.Item(1)

	' Create a Proxy of the Origin Point WorkPoint
	Dim oWPoint_Origin_Point_Proxy As WorkPointProxy = Nothing
	oOcc.CreateGeometryProxy(oWPoint_Origin_Point, oWPoint_Origin_Point_Proxy)

	' Check if the Occurrence should be moved to a WorkPoint other than the Origin Point WorkPoint
	If sWorkPointName <> "" AndAlso sWorkPointName <> "Center Point" Then
		Try
			' Get the Insertion Point WorkPoint
			Dim oWPoint_Insertion_Point As WorkPoint = oOccDoc.ComponentDefinition.WorkPoints.Item(sWorkPointName)

			' Create a Proxy of the Insertion Point WorkPoint 
			Dim oWPoint_Insertion_Point_Proxy As WorkPointProxy = Nothing
			oOcc.CreateGeometryProxy(oWPoint_Insertion_Point, oWPoint_Insertion_Point_Proxy)

			' Get the translation between the two WorkPoints
			Dim dTranslation_X As Double = oWPoint_Origin_Point_Proxy.Point.X - oWPoint_Insertion_Point_Proxy.Point.X
			Dim dTranslation_Y As Double = oWPoint_Origin_Point_Proxy.Point.Y - oWPoint_Insertion_Point_Proxy.Point.Y
			Dim dTranslation_Z As Double = oWPoint_Origin_Point_Proxy.Point.Z - oWPoint_Insertion_Point_Proxy.Point.Z

			' Set the translation Matrix
			Dim oTranslatedMatrix As Matrix = oOcc.Transformation
			oTranslatedMatrix.SetTranslation(g_inventorApplication.TransientGeometry.CreateVector(oWPoint_Origin_Point_Proxy.Point.X + dTranslation_X, oWPoint_Origin_Point_Proxy.Point.Y + dTranslation_Y, oWPoint_Origin_Point_Proxy.Point.Z + dTranslation_Z))

			' Set the transformation of the Component Occurrence
			oOcc.Transformation = oTranslatedMatrix
		Catch
			MsgBox("Work Point """ & sWorkPointName & """ was not found, the Occurrence was placed using the Origin Point")
		End Try
	End If

	' Return the Component Occurrence
	oResult = oOcc
	Return oResult
End Function

Public Shared Function CreateMatrixFromLocationAndRotation(ByVal Location_X As Double, ByVal Location_Y As Double, ByVal Location_Z As Double, ByVal Rotation_X As Double, ByVal Rotation_Y As Double, ByVal Rotation_Z As Double) As Matrix
	Dim oResult As Matrix

	' Set a reference to the Transient Geometry
	Dim oTransientGeometry As TransientGeometry = g_inventorApplication.TransientGeometry

	' Create Matrices
	Dim oMatrix As Matrix = oTransientGeometry.CreateMatrix
	Dim oMatrix_Z As Matrix = oTransientGeometry.CreateMatrix
	Dim oMatrix_Y As Matrix = oTransientGeometry.CreateMatrix
	Dim oMatrix_X As Matrix = oTransientGeometry.CreateMatrix

	' Set the rotation of the Matrix about the Z axis
	oMatrix_Z.SetToRotation(Rotation_Z * (Math.PI / 180), oTransientGeometry.CreateVector(0, 0, 1), oTransientGeometry.CreatePoint(0, 0, 0))

	' Set the rotation of the Matrix about the Y axis
	oMatrix_Y.SetToRotation(Rotation_Y * (Math.PI / 180), oTransientGeometry.CreateVector(0, 1, 0), oTransientGeometry.CreatePoint(0, 0, 0))

	' Set the rotation of the Matrix about the X axis
	oMatrix_X.SetToRotation(Rotation_X * (Math.PI / 180), oTransientGeometry.CreateVector(1, 0, 0), oTransientGeometry.CreatePoint(0, 0, 0))

	' PreMultiply the Matrix by the Matrices about each axis
	oMatrix.PreMultiplyBy(oMatrix_Z)
	oMatrix.PreMultiplyBy(oMatrix_Y)
	oMatrix.PreMultiplyBy(oMatrix_X)

	' Set the Translation of the matrix
	oMatrix.SetTranslation(oTransientGeometry.CreateVector(Location_X / 10, Location_Y / 10, Location_Z / 10))

	' Return the Component Occurrence
	oResult = oMatrix

	Return oResult
End Function

 

For now the main code is made for an iLogic rule because it allowed me to easily test it, but eventually it should all go in my add-in

0 Likes
150 Views
0 Replies
Replies (0)