Wednesday, January 23, 2013

iLogic: Delete All Sick Assembly Constraints

Issue:
You sometimes end up with a large number of broken (sick) assembly constraints, due to a particular workflow (such as swapping out a base component). You know why this is happening and don't mind fixing the assembly, but you wish there was a way to delete all of the sick assembly constraints at once, so you can re-constrain the assembly without having to be bothered by the alerts.



Solution:
Here is a quick iLogic snippet to help with this:

* A special thanks to Brendan Henderson for letting me know about a coding error I had when I first posted this. Check out Brendan's own blog at: http://www.blh.com.au/#/blog/4572268941             






Dim oAssDoc As AssemblyDocument
oAssDoc = ThisApplication.ActiveDocument
Dim oConstraint As AssemblyConstraint

RUSure = MessageBox.Show _
("Are you sure you want to Delete all sick constraints?",  _
"iLogic",MessageBoxButtons.YesNo)

If RUSure = vbNo Then
Return
Else
          i = 0
          For Each oConstraint In oAssDoc.ComponentDefinition.Constraints
            If oConstraint.HealthStatus <> oConstraint.HealthStatus.kUpToDateHealth And _
            oConstraint.HealthStatus <> oConstraint.HealthStatus.kSuppressedHealth Then
          oConstraint.Delete
            i = i + 1
          End If
          Next
End If
MessageBox.Show(" A total of "&  i & " constraints were deleted.", "iLogic")

Thursday, January 17, 2013

iLogic Solid Body Visibility



Issue:
You want to be able to control the visibility of Solid Bodies in a Multi Body part file using  your iLogic rule.




Solution:
Here is a quick example that demonstrates several options to toggle the visibility of solid bodies on and off.




'define the document as a component definition
Dim oCompDef as ComponentDefinition
oCompDef = ThisDoc.Document.ComponentDefinition

'define the solidbody
Dim oBody as SurfaceBody

'add each solid body name to a list
Dim MyArrayList As New ArrayList
i = 1
For Each SurfaceBody in oCompDef.SurfaceBodies
oBody = oCompDef.SurfaceBodies.Item(i)
MyArrayList.add(oBody.Name)
i = i +1
Next

'add Show and Hide all options to the list
MyArrayList.add(" *Show All")
MyArrayList.add(" *Hide All")

'present the user with the list and record the selection
sBody  = InputListBox("Select a Solid Body to Toggle Visibility.", _
MyArrayList, "", "iLogic", "List of Solid Bodies")

'toggle all on or all off, if those options were selected
If sBody = " *Show All" Then
          i = 1
          For Each SurfaceBody in oCompDef.SurfaceBodies
            oBody = oCompDef.SurfaceBodies.Item(i)
          oBody.Visible = True
            i = i +1
          Next
ElseIf sBody = " *Hide All"
            i = 1
          For Each SurfaceBody in oCompDef.SurfaceBodies
            oBody = oCompDef.SurfaceBodies.Item(i)
          oBody.Visible = False
            i = i +1
          Next
End If

'compare the user selection to the solid body names
'and toggle the visibility for the one the matches (if any)
i = 1
For Each SurfaceBody in oCompDef.SurfaceBodies
oBody = oCompDef.SurfaceBodies.Item(i)
If sBody = oBody.Name Then
          If oBody.Visible = False Then
          oBody.Visible = True
            Else
          oBody.Visible = False
            End If
End If
MyArrayList.add(oBody.Name)
i = i +1
Next

Friday, December 21, 2012

iLogic Replace Derived Reference

Issue:
You wish there was an option in parts created from derived components to replace the derived component.



Solution:
Here is a quick iLogic rule that will allow you to browse for a new file with which you can replace the derived reference of an existing derived component.

UPDATE:
A reader recently pointed out a limitation of this iLogic snippet stemming from its use of the API's FileDescriptor.ReplaceReference method. This method requires that the file being replaced and
the replacement file must share ancestry, that is to say that one file was saved from the other file, or both files were saved from the same "parent" file. Simply being aware of this will result in predictable results, but if you were attempting to use this snippet to replace two completely different files you might not have been able to get this to work.

Thanks to Peter for taking the time to bring my attention to this!


Dim oDoc as Document
oDoc = ThisDoc.Document
Dim oRefFile As FileDescriptor
Dim oOrigRefName As Object     

For Each oRefFile In oDoc.file.ReferencedFileDescriptors
'get the full file path to the original internal references
oOrigRefName = oRefFile.FullFileName

'present a File Selection dialog
Dim oFileDlg As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.InitialDirectory = oOrigRefName
oFileDlg.CancelError = True
On Error Resume Next
oFileDlg.ShowOpen()
If Err.Number <> 0 Then
Return
ElseIf oFileDlg.FileName <> "" Then
selectedfile = oFileDlg.FileName
End if

‘replace the reference
oRefFile.ReplaceReference (selectedfile)     
InventorVb.DocumentUpdate()
oOrigRefName = “”                                        
Next

iLogicVb.UpdateWhenDone = True