Possible to assign a specific line type based on parts in a certain browser folder?

Possible to assign a specific line type based on parts in a certain browser folder?

kwilson2D66D9
Explorer Explorer
101 Views
3 Replies
Message 1 of 4

Possible to assign a specific line type based on parts in a certain browser folder?

kwilson2D66D9
Explorer
Explorer

Is it possible to assign a specific layer / line type to all drawing views of a component that resides in a specific folder?

 

Every top-level assembly design we do we have to manually assign the "Dotted" line type to one of our components in a named folder in the browser called "Structure". We do this by right clicking on the model in the Structure folder, click Properties..., in the Component Properties dialog box we uncheck "By Layer" and then  manually assign the "Dotted" line type via the dropdown. I would love to be able to automate this with iLogic so that our colleagues don't have to remember this manual step because some are forgetting this step.

 

Any help or guidance would be much appreciated!

 

browser.jpg

 

linetype.jpg

 

 

0 Likes
Accepted solutions (2)
102 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Hi @kwilson2D66D9.  Yes...sort of, but it is not nearly as simple to make happen by code as it is to do manually.  There would be many steps involved, and a couple different forks in the road, as to which way we could do it, depending on the details of your preferences.  And what we do by code will generally only affect or be applied to individual views in the drawing, but the code can do the same process for each/all views in the drawing, if necessary.   But lets get a couple questions out of the way first.

  • How to find/get the component occurrence
    • Will that folder always exist, and its name always spelled the same, and its letter capitalization always the same?
    • Is there always just one occurrence in that folder?
      • If more than one may be in the folder, then how to find the one you want in that folder?
  • Does Line Weight or Color matter?
  • Will the view always be referencing the parent assembly, and not just that one component directly?

Navigating the 'model browser tree' by code in a drawing is more complicated than doing so in a model file.  The same component can sometimes be found in multiple places, and under multiple drawing view nodes, therefore it is sometimes easier to find something like that in the referenced model file instead of the drawing's own browser tree (by code).

One of the two main 'branches' in the code path is to apply changes to each individual drawing view curve it finds belonging to that one occurrence, which takes much longer to process.  The other main branch collects all of the drawing view curves that we need to change, and sets them all to another 'Layer' at one time, which processes much faster.  But the 'alternate' Layer needed may not exist in that drawing yet, so it would usually need to be created first (can be done by code), before we can set drawing view curves to that Layer.  And of course a Layer has several different specifications.  Either way, these types of changes can be difficult to change again later, if you change your mind about something, or if the model updates, which may change things in the drawing view.  There have been several topics on this forum about similar tasks, and many of them already have 'accepted solutions'.  They may give you some ideas about what is involved.  But we can also help out here too.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

WCrihfield
Mentor
Mentor
Accepted solution

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

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 4

navya_gelli
Autodesk
Autodesk

Hello @kwilson2D66D9

 

Have you had a chance to review WCrihfield reply? Did it help resolve your question?
If it did, please consider marking it as the accepted solution so others can easily find it. If not, feel free to share an update and the community will be happy to assist further.

Navya | Community Manager
0 Likes