ilogic : changing layers, text colour and dimension layers allin

ilogic : changing layers, text colour and dimension layers allin

denis7BY3C
Explorer Explorer
1,618 Views
1 Reply
Message 1 of 2

ilogic : changing layers, text colour and dimension layers allin

denis7BY3C
Explorer
Explorer

Hi, 

I want to change layers with ilogic code . Is this possible? 

for example:

steel, steel hidden and steel view layers change to SKS20H SKS10H and SKS30H layers (existing layers). Also , need code for  changing the colour of dimension (put to another layer ). Below is code (found on this forum), and changing the colour of text (for example TOP VIEW-label)

Thanks!

SyntaxEditor Code Snippet

ub Main
    drawDoc = TryCast(ThisDoc.Document, DrawingDocument)
    If (drawDoc Is Nothing) Then
       MessageBox.Show("This rule can only be run in a drawing,", "iLogic")
       Return
    End If
    ChangeLayerOfOccurrenceCurves()
End Sub

Private drawDoc As DrawingDocument
Private layersNotFound As New HashSet(Of String)

Public Sub ChangeLayerOfOccurrenceCurves()
	' 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 by Materials")
	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
				' Call the recursive function that does all the work. 
				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
			Next
		Next
	Catch
		trans.Abort()
		Throw
	End Try
	trans.End()

	If (layersNotFound.Count > 0) Then
		Dim message As String = "The following layers were not found:" + vbNewLine + vbNewLine
		For Each s As String In layersNotFound
			message += s + vbNewLine
		Next
		MessageBox.Show(message, "Drawing Layers")
	End If
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
		' Check to see if this occurrence is a part or assembly. 
		If occ.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
			' ** It's a part, so get the material.
			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.GetOrCreateLayers(layersNotFound)
				matLayers.SetDrawingCurvesLayer(ThisApplication)
			End If
		Else
			' It's an assembly, so process its contents. 
			Call ProcessOccurrences(drawView, occ.SubOccurrences)
		End If
	Next
End Sub

Sub ProcessPart(ByVal drawView As DrawingView, ByVal partDoc As PartDocument)
	Dim drawCurves As DrawingCurvesEnumerator = Nothing
	Try
		drawCurves = drawView.DrawingCurves()
	Catch
		drawCurves = Nothing
	End Try
	If (drawCurves IsNot Nothing) Then
		Dim matLayers As New MaterialLayers(drawDoc, partDoc, drawView, drawCurves)
		matLayers.GetOrCreateLayers(layersNotFound)
		matLayers.SetDrawingCurvesLayer(ThisApplication)
	End If
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

	Private materialName As String

	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

	Public Sub GetOrCreateLayers(ByVal layersNotFound As HashSet(Of String))
		GetOrCreateMainLayer(layersNotFound)
		If HiddenLayerIsRequired() Then
			GetOrCreateHiddenLayer(layersNotFound)
		End If
	End Sub

	Sub GetOrCreateMainLayer(ByVal layersNotFound As HashSet(Of String))
		materialName = partDoc.ComponentDefinition.Material.Name

		' Verify that a layer exists for this material. 
		Dim layers As LayersEnumerator = drawDoc.StylesManager.Layers
		Dim layerName As String = materialName
		If (drawView.ViewType <> DrawingViewTypeEnum.kStandardDrawingViewType And drawView.ViewType <> DrawingViewTypeEnum.kProjectedDrawingViewType) Then
			layerName += "-View"
		End If

		Dim newLayer As Layer = Nothing
		Try
			newLayer = layers.Item(layerName)
		Catch
			layersNotFound.Add(layerName)
		End Try

		MainLayer = newLayer
	End Sub

	Sub GetOrCreateHiddenLayer(ByVal layersNotFound As HashSet(Of String))
		If (MainLayer Is Nothing) Then Return
		Dim layerName As String = MainLayer.Name + "-Hidden"
		Dim layers As LayersEnumerator = drawDoc.StylesManager.Layers

		Dim newLayer As Layer = Nothing
		Try
			newLayer = layers.Item(layerName)
		Catch
		End Try

		If (newLayer Is Nothing) Then
			' Copy the main layer for this material to create a hidden layer.
			newLayer = MainLayer.Copy(layerName)
			newLayer.LineType = LineTypeEnum.kDashedHiddenLineType
			newLayer.LineWeight = MainLayer.LineWeight * 0.7
		End If
		HiddenLayer = newLayer
	End Sub

	Function HiddenLayerIsRequired() As Boolean
		For Each drawCurve As DrawingCurve In drawCurves
			For Each segment As DrawingCurveSegment In drawCurve.Segments
				If (segment.HiddenLine) Then
					Return True
				End If
			Next
		Next
		Return False
	End Function

	Sub SetDrawingCurvesLayer(ByVal app As Inventor.Application)
		If (MainLayer Is Nothing) Then Return
		If (drawCurves Is Nothing) Then Return
		' Create an empty collection. 
		Dim objColl As ObjectCollection = app.TransientObjects.CreateObjectCollection()
		Dim hiddenColl As ObjectCollection = Nothing
		If (HiddenLayer IsNot Nothing) Then
			hiddenColl = app.TransientObjects.CreateObjectCollection()
		End If

		' Add the curve segments to the collection. 
		For Each drawCurve As DrawingCurve In drawCurves
			For Each segment As DrawingCurveSegment In drawCurve.Segments
				If (Not ShouldChangeLayer(segment)) Then Continue For
				If (segment.HiddenLine) Then
					If (hiddenColl IsNot Nothing) Then
						hiddenColl.Add(segment)
					End If
				Else
					objColl.Add(segment)
				End If
			Next
		Next

		' Change the layer of all of the segments.
		If (objColl.Count > 0) Then
			drawView.Parent.ChangeLayer(objColl, MainLayer)
		End If
		If (hiddenColl IsNot Nothing AndAlso hiddenColl.Count > 0) Then
			drawView.Parent.ChangeLayer(hiddenColl, HiddenLayer)
		End If
	End Sub

	Function ShouldChangeLayer(ByVal segment As DrawingCurveSegment) As Boolean
		If (Not segment.Layer.Name.StartsWith(materialName, StringComparison.OrdinalIgnoreCase)) Then Return True
		If (segment.HiddenLine <> LayerIsHidden(segment.Layer)) Then Return True
		Return False
	End Function

	Shared Function LayerIsHidden(ByVal layerA As Layer) As Boolean
		Return layerA.InternalName.Contains("Hidden")
	End Function

End Class

 

0 Likes
1,619 Views
1 Reply
Reply (1)
Message 2 of 2

chandra.shekar.g
Autodesk Support
Autodesk Support

@denis7BY3C,

 

Below blog article will help to know about creating layer, assigning a color and applying layer to textbox.

 

http://adndevblog.typepad.com/manufacturing/2012/05/changing-an-objects-layer-in-a-drawingdocument.h...

 

For more understanding, can you please provide sample drawing document with screenshots?

 

Please make sure that files are non confidential.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes