Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Changing all line layers of a subassembly

will_roe
Enthusiast

Changing all line layers of a subassembly

will_roe
Enthusiast
Enthusiast

I use the below to grey out certain parts in an assembly. I could of sworn this used to work on sub-assemblies but now it doesn't am I wrong?

 

 

Class ThisRule
	Public oTargetName As String
	Public oTargetLayer As String

	Sub Main

		oTargetName = "M21-TEST-001"
		oTargetLayer = "TEST"

		If ThisApplication.ActiveDocument.DocumentType <> _
		DocumentTypeEnum.kDrawingDocumentObject Then Return 'exit rule

		' Get the active drawing document. 
		Dim oDoc As DrawingDocument
		oDoc = ThisApplication.ActiveDocument

		Dim oLayers As LayersEnumerator
		oLayers = oDoc.StylesManager.Layers

		Dim oLayer As Layer
		For Each xLayer In oLayers
			If xLayer.name = oTargetLayer Then
				oLayer = xLayer
				Exit For
			End If
		Next

		If oLayer Is Nothing Then
			MsgBox("No layer named """ & oTargetLayer & """ was found in this drawing." _
			& vbLf & vbLf & "Cannot continue.", , "iLogic")
			Exit Sub
		End If

		Dim oSheets As Sheets
		oSheets = oDoc.Sheets
		Dim oSheet As Sheet

		'get current sheet so it can
		'be made active again later
		Dim oCurrentSheet As Sheet
		oCurrentSheet = oDoc.ActiveSheet

		Dim oViews As DrawingViews
		Dim oView As DrawingView

		' Iterate through the sheets
		For Each oSheet In oSheets
			'activate the sheet
			oSheet.Activate

			'get the collection of view on the sheet
			oViews = oSheet.DrawingViews

			' Iterate through the views on the sheet
			For Each oView In oViews

				If oView.ReferencedDocumentDescriptor Is Nothing Then Continue For
					
				Dim docDesc As DocumentDescriptor
				docDesc = oView.ReferencedDocumentDescriptor

				' Verify that the drawing view is of an assembly. 
				If docDesc.ReferencedDocumentType <> kAssemblyDocumentObject Then
					Continue For
				End If

				' Get the component definition for the assembly. 
				Dim asmDef As AssemblyComponentDefinition
				asmDef = docDesc.ReferencedDocument.ComponentDefinition

				' Process the view, wrapping it in a transaction so the 
				' each view can be undone with a single undo operation. 
				Dim trans As Transaction
				trans = ThisApplication.TransactionManager.StartTransaction( _
				oDoc, "Change drawing view color")

				' Call the recursive function that does all the work. 
				Call ProcessAssemblyColor(oView, asmDef.Occurrences)
				trans.End
			Next
			'update the sheet
			oSheet.Update
		Next

		'return to original sheet	
		oCurrentSheet.Activate

	End Sub


	Private Sub ProcessAssemblyColor(drawView As DrawingView, _
		Occurrences As ComponentOccurrences)

		Logger.Info("------------" & drawView.Name)
		' Iterate through the current collection of occurrences. 
		Dim occ As ComponentOccurrence
		For Each occ In Occurrences
			' Check to see if this occurrence is a part or assembly. 
			If occ.DefinitionDocumentType = kPartDocumentObject Then
				If Not occ.Name.Contains(oTargetName) Then Continue For
				Logger.Info(occ.Name)
				' ** It's a part so process the layer

				' Get the TransientsObjects object to use later. 
				Dim transObjs As TransientObjects
				transObjs = ThisApplication.TransientObjects

				Dim oLayers As LayersEnumerator
				oLayers = drawView.Parent.Parent.StylesManager.layers

				Dim oLayer As Layer
				oLayer = oLayers.Item(oTargetLayer)

				Dim oDoc As DrawingDocument
				oDoc = drawView.Parent.Parent

				' Get all of the curves associated with this occurrence. 
				On Error Resume Next
				Dim drawcurves As DrawingCurvesEnumerator
				drawcurves = drawView.DrawingCurves(occ)
				If Err.Number = 0 Then
					On Error GoTo 0

					' Create an empty collection. 
					Dim objColl As ObjectCollection
					objColl = transObjs.CreateObjectCollection()

					' Add the curve segments to the collection. 
					Dim drawCurve As DrawingCurve
					For Each drawCurve In drawcurves
						Dim segment As DrawingCurveSegment
						For Each segment In drawCurve.Segments
							objColl.Add(segment)
						Next
					Next

					' Change the layer of all of the segments. 
					Call drawView.Parent.ChangeLayer(objColl, oLayer)
				End If
				On Error GoTo 0
			ElseIf occ.DefinitionDocumentType = kAssemblyDocumentObject Then
				' It's an assembly so process its contents. 
				Call ProcessAssemblyColor(drawView, occ.SubOccurrences)
			Else
				Continue For
			End If
		Next
	End Sub
End Class

 

0 Likes
Reply
Accepted solutions (1)
260 Views
1 Reply
Reply (1)

WCrihfield
Mentor
Mentor
Accepted solution

Hi @will_roe.  Most of this looks OK, but there are a few places I might have done things a little differently.  I know that top level components (ComponentOccurrence objects), and that ComponentOccurrenceProxy objects will work as input to the DrawingView.DrawingCurves property/method, but I believe that those proxy versions need to be in the context of the top level assembly.  I don't know how much you may already know about proxies and how they work, but it is a fairly complex/advanced topic.  If the component is in a sub-assembly, then that component is seen as a ComponentOccurrenceProxy from the perspective of the top level assembly (or from the parent of the sub-assembly, if deeper).  And if the component is more than one layer deep, you would need to essentially step up that proxy ladder to get to the top level.  What I mean by that is:  get the real ComponentOccurrence object that is within the sub-sub assembly, then use its CreateGeometryProxy method get the proxy of it that is now in the context of the next level parent assembly (sub assembly now), then use the component that represents the sub-assembly, and use its CreateGeometryProxy method to get a reference to the proxy at its parent assembly level, until the proxy's geometry is within the context (the 3D coordinate space) of the top level assembly.  Then you can use that top level proxy to get the drawing curves.  It can be a lot of work by code.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes