Thursday, May 10, 2012

Working With Unconstrained Imported Assembly Components




Issue:
You've found a great 3D model on the web that you want to use in Inventor. It was originally created in some other 3D design software, but you were able to import it into Inventor. The problem is that the parts come in unconstrained and can move all over the place. Is there an option to import models from other software and keep the assembly constraints?

Solution:
There is no option to import the assembly constraints, but most likely you won't need to apply constraints to all of the parts because they have been imported in the correct orientation and location. Instead you can just ground all of the parts in place so that they will not move, and then un-ground and apply assembly constraints to just the parts that need to move or that you need to edit.

If the assembly has a lot of moving parts that need assembly constraints, you can un-ground and constrain one part at a time to keep everything from moving on you.

Here is a quick iLogic rule that will ground all components in place also.


' get the active assembly
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'set the Master LOD active
Dim oLODRep As LevelofDetailRepresentation
oLODRep = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("Master")
oLODRep.Activate

'Iterate through all of the occurrences and ground them.
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
	'check for and skip virtual components
	If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
		oOccurrence.Grounded = True
	Else
	End If
Next



Update : 
Here is an updated version of this rule that grounds everything in all assembly levels.

Sub Main

	Call TraverseAssembly(ThisDoc.Document.ComponentDefinition.Occurrences)

End Sub

Function TraverseAssembly(oOccs As ComponentOccurrences)
	Dim oOcc As ComponentOccurrence
	For Each oOcc In oOccs
		Try : oOcc.Grounded = True: Catch : End Try
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			Call TraverseAssembly(oOcc.SubOccurrences)
		End If
	Next
End Function 



Another version that offers the user a choice to ground or unground:

Sub Main
	oOccs = ThisDoc.Document.ComponentDefinition.Occurrences
	oInput = InputRadioBox("Select one", "Ground All", "Unground All", True,"iLogic")
	Call TraverseAssembly(ThisDoc.Document.ComponentDefinition.Occurrences, oInput)
End Sub

Function TraverseAssembly(oOccs As ComponentOccurrences, oInput As Boolean)
	Dim oOcc As ComponentOccurrence
	For Each oOcc In oOccs
		Try : oOcc.Grounded = oInput : Catch : End Try
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			Call TraverseAssembly(oOcc.SubOccurrences, oInput)
		End If
	Next
End Function 


Friday, March 2, 2012

iLogic to Select all of the Closed Profiles Found in a Sketch

Issue:
You find yourself needing to select a large number of closed profiles in a sketch of a logo or something similar. This is quite tedious and because you reuse the sketch over and over, you find yourself wishing you had a better way to do this.

Here is an example of a logo that was copied from AutoCAD and placed in an Inventor sketch block. It's a bit tedious to select all of these profiles each time the logo is used on a laser engraved part.



Solution:
You can use the following iLogic rule to select all of the closed profiles found in the sketch, and create an Extrude feature from them.




'----start of ilogic -------
If Typeof ThisApplication.ActiveEditObject Is Sketch Then
'Do nothing
Else
MessageBox.Show("Activate a Sketch First then Run this Rule", "ilogic")
Return
End If

Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument

Dim oCompDef As PartComponentDefinition
oCompDef = oPartDoc.ComponentDefinition

Dim oSketch As PlanarSketch
oSketch = ThisApplication.ActiveEditObject

' Create a profile.
Dim oProfile As Profile
On Error Goto NoProfile
oProfile = oSketch.Profiles.AddForSolid

'get user input
oDistance = InputBox("Enter Extrude Distance", "iLogic", "10 mm")
oDirection  = InputRadioBox("Select Extrude Direction", "Up (+)", "Down (-)", True, Title := "iLogic")
oJoinOrCut  = InputRadioBox("Select Extrude Solution", "Join", "Cut", True, Title := "iLogic")

If oDirection = True then
oDirection = kPositiveExtentDirection
Else
oDirection = kNegativeExtentDirection
End if

If oJoinOrCut = True then
oJoinOrCut = kJoinOperation
Else
oJoinOrCut = kCutOperation
End if

' Create an extrusion
Dim oExtrude As ExtrudeFeature
On Error Goto NoExtrude
oExtrude = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent( _
oProfile, oDistance, oDirection, oJoinOrCut)

ThisApplication.CommandManager.ControlDefinitions.Item("FinishSketch").Execute

iLogicVb.UpdateWhenDone = True

exit sub

NoProfile:
MessageBox.Show("No closed profile found", "iLogic")
Return

NoExtrude:
MessageBox.Show("No extrusion created, check your inputs.", "iLogic")
Return
'----end of ilogic -------


 ---------------------------------------------------------------------------------------------------------------------
Update ( 8/21/2012 ):

By request here is another version of this rule that automatically cuts through all:

'----start of ilogic -------
If Typeof ThisApplication.ActiveEditObject Is Sketch Then
'Do nothing
Else
MessageBox.Show("Activate a Sketch First then Run this Rule", "ilogic")
Return
End If

Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument

Dim oCompDef As PartComponentDefinition
oCompDef = oPartDoc.ComponentDefinition

Dim oSketch As PlanarSketch
oSketch = ThisApplication.ActiveEditObject

' Create a profile.
Dim oProfile As Profile
On Error Goto NoProfile
oProfile = oSketch.Profiles.AddForSolid

'set direction
oDirection  = kSymmetricExtentDirection
'set operation
oJoinOrCut  = kCutOperation

' Create an extrusion
Dim oExtrude As ExtrudeFeature
On Error Goto NoExtrude
oExtrude = oCompDef.Features.ExtrudeFeatures.AddByThroughAllExtent( _
oProfile, oDirection, oJoinOrCut)

ThisApplication.CommandManager.ControlDefinitions.Item("FinishSketch").Execute

iLogicVb.UpdateWhenDone = True

exit sub

NoProfile:
MessageBox.Show("No closed profile found", "iLogic")
Return

NoExtrude:
MessageBox.Show("No extrusion created, check your inputs.", "iLogic")
Return
'----end of ilogic ------- 



------------------------------------------------------------------------------------------------------------------------------------------
Update ( 8/21/2012 ):

By request here is another version of this rule that extrudes using the To Next method:



'----start of ilogic -------
If Typeof ThisApplication.ActiveEditObject Is Sketch Then
'Do nothing
Else
MessageBox.Show("Activate a Sketch First then Run this Rule", "ilogic")
Return
End If

Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument

Dim oCompDef As PartComponentDefinition
oCompDef = oPartDoc.ComponentDefinition

Dim oSketch As PlanarSketch
oSketch = ThisApplication.ActiveEditObject

' Create a profile.
Dim oProfile As Profile
On Error Goto NoProfile
oProfile = oSketch.Profiles.AddForSolid

oDirection  = InputRadioBox("Select Extrude Direction", "Up (+)", "Down (-)", True, Title := "iLogic")
oJoinOrCut  = InputRadioBox("Select Extrude Solution", "Join", "Cut", True, Title := "iLogic")

If oDirection = True then
oDirection = kPositiveExtentDirection
Else
oDirection = kNegativeExtentDirection
End if

If oJoinOrCut = True then
oJoinOrCut = kJoinOperation
Else
oJoinOrCut = kCutOperation
End if

Dim oTerminator As SurfaceBody
oTerminator = oCompDef.SurfaceBodies(1)

' Create an extrusion
Dim oExtrude As ExtrudeFeature
On Error Goto NoExtrude
oExtrude = oCompDef.Features.ExtrudeFeatures.AddByToNextExtent( _
oProfile, oDirection, oTerminator, oJoinOrCut)

ThisApplication.CommandManager.ControlDefinitions.Item("FinishSketch").Execute
iLogicVb.UpdateWhenDone = True

exit sub

NoProfile:
MessageBox.Show("No closed profile found", "iLogic")
Return

NoExtrude:
MessageBox.Show("No extrusion created, check your inputs.", "iLogic")
Return
'----end of ilogic -------


------------------------------------------------------------------------------------------------------------------------------------------
Update ( 5/14/2014 ):



By request here is another version of this rule that creates a revolved feature, revolved around the X axis:




'----start of ilogic -------
If Typeof ThisApplication.ActiveEditObject Is Sketch Then
'Do nothing
Else
MessageBox.Show("Activate a Sketch First then Run this Rule", "ilogic")
Return
End If

Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument
Dim oCompDef As PartComponentDefinition
oCompDef = oPartDoc.ComponentDefinition
Dim oSketch As PlanarSketch
oSketch = ThisApplication.ActiveEditObject
Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry

' Create a profile.
Dim oProfile As Profile
On Error Goto NoProfile
oProfile = oSketch.Profiles.AddForSolid

'Create Centerline
Dim points(2) As SketchPoint
points(0) = oSketch.SketchPoints.Add(oTG.CreatePoint2d(0, 0), False)
points(1) = oSketch.SketchPoints.Add(oTG.CreatePoint2d(5, 0), False)
Dim oCenterLine As SketchLine
oCenterLine = oSketch.SketchLines.AddByTwoPoints(points(0), points(1))

' Create the revolved feature
Dim oRev As RevolveFeature
On Error Goto NoFeature
oRev = oCompDef.Features.RevolveFeatures.AddFull(oProfile, oCenterLine, kJoinOperation)
ThisApplication.CommandManager.ControlDefinitions.Item("FinishSketch").Execute
iLogicVb.UpdateWhenDone = True

exit sub
NoProfile:
MessageBox.Show("No closed profile found", "iLogic")
Return

NoFeature:
MessageBox.Show("No feature created, check your inputs.", "iLogic")
Return
'----end of ilogic -------