Message 1 of 6
layer change in drawing with respect to category specified in subassembly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Dear All, I have prepared an ilogic rule to change layer in drawing as per bill of material, now I need help to change it VIA category (iPoperties>Summary>Category)
If Category contains
X1 then change that with AM_2
X2 then change that with AM_1
X3 then change that with AM_11
X4 then change that with AM_5
Sub Main()
' Ensure the active document is a drawing document
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
Call MsgBox("This macro only works on drawing documents.", vbExclamation, "Error")
Exit Sub
End If
' Get the active drawing document
Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveDocument
' Get all layers in the drawing
Dim oLayers As LayersEnumerator
oLayers = oDoc.StylesManager.Layers
' Define the target layer
Dim TargetLayer As String
TargetLayer = "AM_1" ' Change this to your desired layer name
' Find the target layer
Dim oLayer As Layer = Nothing
For Each xLayer As Layer In oLayers
If xLayer.Name = TargetLayer Then
oLayer = xLayer
Exit For
End If
Next
' If the target layer doesn't exist, show an error and exit
If oLayer Is Nothing Then
Call MsgBox("Layer '" & TargetLayer & "' not found.", vbExclamation, "Error")
Exit Sub
End If
' Start a transaction for undo support
Dim oTrans As Transaction
oTrans = ThisApplication.TransactionManager.StartTransaction(oDoc, "Move Purchased Parts to Layer")
' Loop through all sheets in the drawing
For Each sheet As Sheet In oDoc.Sheets
For Each view As DrawingView In Sheet.DrawingViews
' Check if the view has a valid reference
If View.ReferencedDocumentDescriptor Is Nothing Then
Continue For
End If
' Get the component definition from the view
Dim refAssyDef As ComponentDefinition
If View.ReferencedDocumentDescriptor.ReferencedDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
refAssyDef = View.ReferencedDocumentDescriptor.ReferencedDocument.ComponentDefinition
Else
Continue For
End If
' Ensure the reference is valid
If refAssyDef Is Nothing Then
Continue For
End If
' Process all occurrences (including subassemblies)
ProcessOccurrences(refAssyDef.Occurrences, View, oLayer)
Next
Next
' End the transaction
oTrans.End()
' Notify the user
Call MsgBox("Purchased parts moved to layer '" & TargetLayer & "'.", vbInformation, "Completed")
End Sub
' --- Recursive Function to Process Subassemblies and Parts ---
Sub ProcessOccurrences(ByVal occurrences As ComponentOccurrences, ByVal view As DrawingView, ByVal oLayer As Layer)
For Each occurrence As ComponentOccurrence In occurrences
' FIX: Ensure we only access SubOccurrences for assemblies
If occurrence.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
Try
' Use `ReferencedDocumentDescriptor.ReferencedDocument` instead of `ReferencedDocument`
Dim refDoc As Document = Nothing
If occurrence.ReferencedDocumentDescriptor IsNot Nothing Then
refDoc = occurrence.ReferencedDocumentDescriptor.ReferencedDocument
End If
' Ensure the referenced document is valid
If refDoc Is Nothing Then
Call MsgBox("Skipping: " & occurrence.Name & " (No valid referenced document)", vbExclamation, "Warning")
Continue For
End If
' Ensure SubOccurrences exist and are not empty
If occurrence.SubOccurrences IsNot Nothing AndAlso occurrence.SubOccurrences.Count > 0 Then
ProcessOccurrences(occurrence.SubOccurrences, view, oLayer)
Else
Call MsgBox("Skipping: " & occurrence.Name & " (No sub-occurrences)", vbExclamation, "Info")
End If
Catch ex As Exception
Call MsgBox("Error accessing subassemblies for: " & occurrence.Name & vbCrLf & ex.Message, vbExclamation, "Error")
Continue For
End Try
Else
' If it's a purchased part, move it to the layer
If occurrence.BOMStructure = BOMStructureEnum.kPurchasedBOMStructure Then
Try
' Check if there are drawing curves for this occurrence
Dim ViewCurves As DrawingCurvesEnumerator = Nothing
If view Is Nothing OrElse occurrence Is Nothing Then
Continue For
End If
' Attempt to get drawing curves
Try
ViewCurves = view.DrawingCurves(occurrence)
Catch
' Skip occurrences that don't have drawing curves
Continue For
End Try
' If no curves exist, continue to the next occurrence
If ViewCurves Is Nothing OrElse ViewCurves.Count = 0 Then
Continue For
End If
' Move the purchased part curves to the target layer
For Each c As DrawingCurve In ViewCurves
For Each segment As DrawingCurveSegment In c.Segments
' Ensure the segment has a valid layer before modifying it
If segment.Layer IsNot Nothing AndAlso segment.Layer.LineType = 37633 Then
segment.Layer = oLayer
End If
Next
Next
Catch ex As Exception
Call MsgBox("Error processing part: " & occurrence.Name & vbCrLf & ex.Message, vbExclamation, "Error")
End Try
End If
End If
Next
End Sub