Below is a rough draft of the code you can use in an iLogic rule which you can start out with for this type of task. There are a lot of opportunities for something to not work as planned, which is why you will see a lot of 'Try...Catch...End Try' statements in there, which 'handle' (block) the usual errors that would normally pop-up when something causes an error. In each of those situations, we can insert some code on the 'Catch' side (like a Logger entry or MsgBox) that will be ran instead, when it catches an error, if you would like more 'feedback' about when those errors happen. When you run this rule, it expects a drawing to be the 'active' (visible on screen for editing) document. If that is not the case, it will simply not do anything. This example will attempt to create a Layer named 'StructureLayer', if it can not find an existing one with that name. If it has to create the Layer, it simply copies the first existing Layer in the drawing that already exists, then changes its Line Style to the 'Dotted' variation, so color & Line Weight may be different. All this stuff can be changed later, once we figure out if it will work. I included the use of a 'Transaction' in this rule, to record all of its actions into one item in the UNDO list, so they can easily be undone, immediately after it finishes, if necessary.
Sub Main
Dim oInvApp As Inventor.Application = ThisApplication
Dim oDDoc As Inventor.DrawingDocument = TryCast(oInvApp.ActiveDocument, Inventor.DrawingDocument)
If oDDoc Is Nothing Then Return
Dim oSheets As Inventor.Sheets = oDDoc.Sheets
Dim oASheet As Inventor.Sheet = oDDoc.ActiveSheet
Dim oDottedLayer As Inventor.Layer = Nothing
Dim oObjColl As Inventor.ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection()
Dim oTrans As Inventor.Transaction = Nothing
oTrans = ThisApplication.TransactionManager.StartTransaction(oDDoc, "Set Component Layer In Views")
Try
For Each oSheet As Inventor.Sheet In oSheets
oObjColl.Clear()
oSheet.Activate()
For Each oDView As DrawingView In oSheet.DrawingViews
'get the Assembly document this view is directly referencing (if any)
Dim oViewDoc As Inventor.AssemblyDocument = Nothing
oViewDoc = TryCast(oDView.ReferencedDocumentDescriptor.ReferencedDocument, Inventor.AssemblyDocument)
If oViewDoc Is Nothing Then Continue For
'get component occurrence
Dim oOcc As Inventor.ComponentOccurrence = Nothing
oOcc = GetOccInBrowserFolder(oViewDoc)
If oOcc Is Nothing Then Continue For
'get the drawing curves belonging to this component occurrence
Dim oDCs As DrawingCurvesEnumerator = Nothing
Try
oDCs = oDView.DrawingCurves(oOcc)
Catch
End Try
If (oDCs Is Nothing) OrElse (oDCs.Count = 0) Then
Continue For
End If
'put all of its segments into our collection for this Sheet
For Each oDCSeg As Inventor.DrawingCurveSegment In oDCs
oObjColl.Add(oDCSeg)
Next 'oDCSeg
Next 'oDView
'get or create the Layer, if we have not already done so
If oDottedLayer Is Nothing Then
oDottedLayer = GetDottedLayer(oDDoc)
If oDottedLayer Is Nothing Then
MsgBox("Could Not Find Or Create Dotted Layer!", vbCritical, "Dotted Layer Needed")
Return
End If
End If
'call method to change Layer of collected drawing curve segments
Try
oSheet.ChangeLayer(oObjColl, oDottedLayer)
Catch
End Try
'update this sheet
oSheet.Update()
Next 'oSheet
'activate the originally active sheet again
oASheet.Activate()
'end the current Transaction normally
oTrans.End()
Catch
oTrans.Abort()
MsgBox("An Error Happened, So It Used UNDO To Reverse Any Changes So Far!", _
vbCritical, "Error Handled - Transaction Aborted")
End Try
End Sub
Function GetOccInBrowserFolder(adoc As Inventor.AssemblyDocument) As Inventor.ComponentOccurrence
Dim oModelPane As Inventor.BrowserPane = adoc.BrowserPanes.Item("AmBrowserArrangement")
Dim oTopNode As Inventor.BrowserNode = oModelPane.TopNode
Dim oStrFolder As Inventor.BrowserFolder = Nothing
Try
oStrFolder = oTopNode.BrowserFolders.Item("Structure")
Catch
End Try
If oStrFolder Is Nothing Then Return Nothing
Dim oFolderNodes As Inventor.BrowserNodesEnumerator = oStrFolder.BrowserNode.BrowserNodes
If (oFolderNodes Is Nothing) OrElse (oFolderNodes.Count = 0) Then Return Nothing
Dim oNO As Object = Nothing
Try
oNO = oFolderNodes.Item(1).NativeObject
Catch
End Try
If oNO Is Nothing Then Return Nothing
Dim oOcc As Inventor.ComponentOccurrence = TryCast(oNO, Inventor.ComponentOccurrence)
Return oOcc
End Function
Function GetDottedLayer(ddoc As Inventor.DrawingDocument) As Inventor.Layer
Dim oLayers As Inventor.LayersEnumerator = ddoc.StylesManager.Layers
Dim oStructureLayer As Inventor.Layer = Nothing
Dim sLayerName As String = "StructureLayer"
Try
oStructureLayer = oLayers.Item(sLayerName)
Catch
End Try
If (oStructureLayer IsNot Nothing) Then Return oStructureLayer
oStructureLayer = oLayers.Item(1).Copy(sLayerName)
oStructureLayer.LineType = Inventor.LineTypeEnum.kDottedLineType
Return oStructureLayer
End Function
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield

(Not an Autodesk Employee)