Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

@WCrihfield 

Follow all code

Sub Main
    drawDoc = TryCast(ThisDoc.Document, DrawingDocument)
	
    If (drawDoc Is Nothing) Then
       MessageBox.Show("Esta regra só pode ser executada em um desenho,", "iLogic")
       Return
    End If
	
	view = InputBox("Digite o Indentificador da Vista:", "iLogic")
	If view = "" Then
		MessageBox.Show("Necessário fornecer Indentificador da Vista", "iLogic")
		Return
	End If
	
	peca = InputBox("Digite o código da Peça:", "iLogic")
	If peca = "" Then
		MessageBox.Show("Necessário fornecer o código de uma peça", "iLogic")
		Return
	End If
	

    LookForView()
End Sub

Private drawDoc As DrawingDocument
Private modelDoc As Document
Private peca As String
Private view As String

Public Sub LookForView()
    ' Process the occurrences, wrapping it in a transaction so the 
    ' entire process can be undone with a single undo operation. 
    Dim trans As Transaction
    trans = ThisApplication.TransactionManager.StartTransaction(drawDoc, "Drawing Layers to Existing")

    Try
        For Each dSheet As Sheet In drawDoc.Sheets
            For Each drawView As DrawingView In dSheet.DrawingViews
                If (drawView.ViewType = DrawingViewTypeEnum.kDraftDrawingViewType) Then Continue For
                
				If drawView.Name.ToString() = view
		            Dim docDesc As DocumentDescriptor
		            docDesc = drawView.ReferencedDocumentDescriptor
					
					If (docDesc Is Nothing) Then Continue For
					If (docDesc.ReferencedDocumentType = DocumentTypeEnum.kAssemblyDocumentObject) Then
						If (docDesc.ReferencedDocument Is Nothing) Then Continue For
						Dim asmDef As AssemblyComponentDefinition
						Dim assemDoc As AssemblyDocument = TryCast(docDesc.ReferencedDocument, AssemblyDocument)
						If (assemDoc) Is Nothing Then Continue For
						asmDef = assemDoc.ComponentDefinition
						If (asmDef Is Nothing) Then Continue For
						If (asmDef.Occurrences Is Nothing) Then Continue For
						ProcessOccurrences(drawView, asmDef.Occurrences)
					ElseIf (docDesc.ReferencedDocumentType = DocumentTypeEnum.kPartDocumentObject) Then
						Dim partDoc As PartDocument = TryCast(docDesc.ReferencedDocument, PartDocument)
						If (partDoc Is Nothing) Then Continue For
							'ProcessPart(drawView, partDoc)
					End If
				End If		
            Next
        Next
    Catch
        trans.Abort()
		Return
    End Try
    trans.End()
End Sub

Sub ProcessOccurrences(ByVal drawView As DrawingView, ByVal Occurrences As ComponentOccurrences)
	' Iterate through the current collection of occurrences. 
	Dim occ As ComponentOccurrence
	For Each occ In Occurrences
		If (occ.Suppressed) Then Continue For
		If (occ.Definition Is Nothing) Then Continue For
		If (occ.ReferencedDocumentDescriptor Is Nothing) Then Continue For
		If (occ.ReferencedDocumentDescriptor.ReferenceMissing) Then Continue For
		
		Dim parentComponent As ComponentOccurrence = occ.ParentOccurrence
		Dim FileName As String
		
		If parentComponent IsNot Nothing
			FileName = parentComponent.Name
		    Dim partes As String() = FileName.Split(":"c)
		    FileName = partes(0).Trim()
		End If
		
		If occ.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
			' ** It's a part, so get the material.
			if FileName <> peca Then Continue For
			Dim partDoc As PartDocument = TryCast(occ.Definition.Document, PartDocument)
			If (partDoc Is Nothing) Then Continue For

			' Get all of the curves associated with this occurrence. 
			Dim drawCurves As DrawingCurvesEnumerator = Nothing
			Try
				drawCurves = drawView.DrawingCurves(occ)
			Catch
				drawCurves = Nothing
			End Try
			If (drawCurves IsNot Nothing) Then

				Dim matLayers As New MaterialLayers(drawDoc, partDoc, drawView, drawCurves)
				matLayers.GetOrCreateMainLayer()
				matLayers.SetDrawingCurvesLayer(ThisApplication)
			End If
		Else
			' It's an assembly, so process its contents. 
			Call ProcessOccurrences(drawView, occ.SubOccurrences)
		End If
	Next
End Sub

Class MaterialLayers
	Public MainLayer As Layer
	Public HiddenLayer As Layer

	' The layer for:
	' Solid lines in a plan or projected view:     MaterialName
	' Hidden lines in a plan or projected view:    MaterialName-Hidden
	' Solid lines in any other view:               MaterialName-View
	' Hidden lines in any other view:              MaterialName-View-Hidden

	Private drawDoc As DrawingDocument
	Private partDoc As PartDocument
	Private drawView As DrawingView
	Private drawCurves As DrawingCurvesEnumerator

	Public Sub New(ByRef drawDoc As DrawingDocument, ByVal partDoc As PartDocument, ByVal drawView As DrawingView, ByVal drawCurves As DrawingCurvesEnumerator)
		Me.drawDoc = drawDoc
		Me.partDoc = partDoc
		Me.drawView = drawView
		Me.drawCurves = drawCurves
	End Sub
	
	Sub GetOrCreateMainLayer()
		Dim layers As LayersEnumerator = drawDoc.StylesManager.Layers
		Try
			ContorniLayer = layers.Item("CONTORNI")
		Catch
		End Try
		
		Try
			NacosteLayer = layers.Item("NASCOSTE")
		Catch
		End Try
		MainLayer = ContorniLayer
		HiddenLayer = NacosteLayer
	End Sub
	
	Sub SetDrawingCurvesLayer(ByVal app As Inventor.Application)
	    If (MainLayer Is Nothing) Then Return
	    If (drawCurves Is Nothing) Then Return
	
	    ' Create empty collections for curves on different layers
	    Dim objColl As ObjectCollection = app.TransientObjects.CreateObjectCollection()
	    Dim hiddenColl As ObjectCollection = Nothing
	    If (HiddenLayer IsNot Nothing) Then
	        hiddenColl = app.TransientObjects.CreateObjectCollection()
	    End If
	
	    ' Iterate through each drawing curve
	    For Each drawCurve As DrawingCurve In drawCurves
	        If (drawCurve.Segments Is Nothing) Then Continue For
				
			drawCurve.Color = MainLayer.Color
			
	        For Each segment As DrawingCurveSegment In drawCurve.Segments
	            If (segment.HiddenLine) Then
	                If (hiddenColl IsNot Nothing) Then
	                    hiddenColl.Add(segment)
	                End If
	            Else
	                ' Add the segment to the collection for visible curves
	                objColl.Add(segment)
	            End If
	        Next
	    Next
	
	    ' Change the layer of visible curves
	    If (objColl.Count > 0) Then
	        drawView.Parent.ChangeLayer(objColl, MainLayer)
	    End If
	
	    ' Change the layer of hidden curves
	    If (hiddenColl IsNot Nothing AndAlso hiddenColl.Count > 0) Then
	        drawView.Parent.ChangeLayer(hiddenColl, HiddenLayer)
	    End If
	End Sub

End Class