update drawing line colour based on part colour

update drawing line colour based on part colour

brotherkennyh
Advocate Advocate
5,893 Views
20 Replies
Message 1 of 21

update drawing line colour based on part colour

brotherkennyh
Advocate
Advocate

Hi,

Is it possible to have the lines in an unshaded view follow the colour of the appearance of a part. I want to be able to see the part colour without having to have shaded views on.

 

Is this possible?

 

Failing that, is there a way to get the colour with iLogic to have the drawing line colours update automatically?

 

Cheers

 

Kenny

0 Likes
Accepted solutions (4)
5,894 Views
20 Replies
Replies (20)
Message 2 of 21

MechMachineMan
Advisor
Advisor

PROTIP: Google-fu will take you far.

 

 

http://lmgtfy.com/?q=Inventor+%2B+update-drawing-line-colour-based-on-part-colour

 

Literally the 4th result that shows up is about this same topic:

 

Read through that thread and you can find a link to a blog with a solution.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 21

brotherkennyh
Advocate
Advocate

I think you are missing the point. I know how to change the colour, and indeed, this did come up once I figured out how google worked.

I want to be able to have the line colour based on the part colour, so that if the part colour changes the line colour will change automatically. I am not simply asking how to change the colour to match. I want the line colour to remain based on the part colour so that if I add new parts or geometry they will also have the line colour follow the part colour.

0 Likes
Message 4 of 21

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi brotherkennyh,

 

The only way to do this currently is via custom code. MechMachineMan was attempting to point you to a VBA macro that will process the drawing views and set the line colors to be the same as the part colors.

 

Here is that macro converted to run as an iLogic rule. In this version it will process each sheet in the drawing, stepping through each view and changing the line color of each part.

 

 

To use this create a new iLogic rule in your drawing and paste in this code. If you need to use this often, you would likely want to create this as an external rule. 

 

 

If you add parts to the assembly, or change a part's color, you would need to run this iLogic rule in the drawing again. So for that reason, it might best to add this rule to the drawing's "before save" event trigger, so that the colors are updated anytime the drawing is saved

 

Give this a go, and let us know if you have any issues running it.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

Sub Main 
	' Get the active drawing document. 
	Dim oDoc As DrawingDocument 
	oDoc = ThisApplication.ActiveDocument 
	
	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	
			
			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) 
   ' 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 
         ' ** It's a part so process the color.  

         ' Get the render style of the occurrence. 
         Dim color As RenderStyle 
         Dim sourceType As StyleSourceTypeEnum 
         color = occ.GetRenderStyle(sourceType)  

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

         ' Verify that a layer exists for this color. 
         Dim layers As LayersEnumerator 
         layers = drawView.Parent.Parent.StylesManager.layers  

         Dim oDoc As DrawingDocument 
         oDoc = drawView.Parent.Parent  

         On Error Resume Next 
         Dim colorLayer As Layer 
         colorLayer = layers.Item(color.Name)  

         If Err.Number <> 0 Then 
            On Error Goto 0 
            ' Get the diffuse color for the render style. 
            Dim red As Byte 
            Dim green As Byte 
            Dim blue As Byte  

            ' Create a color object that is the diffuse color. 
            Call color.GetDiffuseColor(red, green, blue) 
            Dim newColor As color 
            newColor = transObjs.CreateColor(red, green, blue)  

            ' Copy an arbitrary layer giving it the name 
            ' of the render style. 
            colorLayer = layers.Item(1).Copy(color.Name) 

            ' the attributes of the layer to use the color, 
            ' have a solid line type, and a specific width. 
            colorLayer.Color = newColor 
            colorLayer.LineType = kContinuousLineType 
            colorLayer.LineWeight = 0.02 
         End If 
         On Error Goto 0  

         ' 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, colorLayer) 
         End If 
         On Error Goto 0 
      Else 
         ' It's an assembly so process its contents. 
         Call ProcessAssemblyColor(drawView, occ.SubOccurrences) 
      End If 
   Next 
End Sub

EESignature

Message 5 of 21

brotherkennyh
Advocate
Advocate

Hi, thanks for the input. A little disappointed there isn't just a setting for this hidden somewhere, but this works great

Cheers

Kenny

0 Likes
Message 6 of 21

tjvz85
Enthusiast
Enthusiast

Hi Curtis,

 

I hope that you can help. Your code should be able to do what I am looking for, with a few adjustments.

 

There will only be assemblies (weldments) in the drawings where this rule needs to run. I would like to have a look at all the Views on only the first Sheet of a Drawing and check if there is a View with the Representation View called "Colour". If it finds a View with the Representation View Name = Colour, then check what Colour the parts in the weldment are (all parts will be the same colour) and write the Colour Name to a Custom iProperty "Colour" in the Drawing file. This iProperty will then be called either in a sketch symbol that is placed on the sheet or in the title block.

 

I found the below piece of code (changed to "Colour") which can get the Representation View Name and be used to specify which view's parts colour must be used. I am not sure if it should be implemented as a check step in the first part of your code that runs through the views, or should it be the first part, or just part of the recursive function.

 

ViewName = ocompdef.RepresentationsManager.ActiveDesignViewRepresentation.Name

If ViewName = "Colour" Then... Seems like this should call ProcessAssemblyColour.

 

I am still a novice with iLogic and API, but I am busy with a VBA and C++ course because I would like to better understand how I can perform custom tasks through iLogic.

 

Regards

0 Likes
Message 7 of 21

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @tjvz85 ,

 

I had a quick go at modifying the above rule to your needs, but I only barely tested it.....  so see if this works for you and post back if not.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Sub Main 
	' Get the active drawing document. 
	Dim oDoc As DrawingDocument 
	oDoc = ThisApplication.ActiveDocument 
	
	Dim oSheet As Sheet
	oSheet = oDoc.ActiveSheet	
	
	Dim oViews As DrawingViews
	Dim oView As DrawingView
	

	'get the collection of view on the sheet
	oViews = oSheet.DrawingViews		
	
	' Iterate through the views on the sheet
	For Each oView In oViews	
		
		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 
		
		'define view rep collection
		Dim oViewReps As DesignViewRepresentations
		oViewReps = asmDef.RepresentationsManager.DesignViewRepresentations
		
		oColourViewRepFound = False
		For Each oViewRep In oViewReps
			If oViewRep.Name = "Colour" Then
				oColourViewRepFound = True 
			End If
		Next
		
		If oColourViewRepFound = True Then
			Call ProcessAssemblyColor(oView, asmDef.Occurrences) 
		End If
	Next

End Sub 



Private Sub ProcessAssemblyColor(drawView As DrawingView, _ 
                                 Occurrences As ComponentOccurrences) 
   ' 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 

         ' Get the render style of the occurrence. 
         Dim color As RenderStyle 
         Dim sourceType As StyleSourceTypeEnum 
         color = occ.GetRenderStyle(sourceType)  
		If color.name IsNot Nothing Then
		 iProperties.Value("Custom", "Colour") = color.name
		 Exit For
	 	End If
			

      End If 
   Next 
End Sub

 

EESignature

Message 8 of 21

tjvz85
Enthusiast
Enthusiast

Perfect! I appreciate it so much. Every time I read through the code, the logic starts to make more and more sense.

Thanks again

Message 9 of 21

tjvz85
Enthusiast
Enthusiast

Hi Curtis,

 

Thanks again for the help with this, I really appreciate it.

 

Something that has come up during implementing your code, it gets the colour of the active design view rep in the assembly file, so if default view rep is active, the value for the "Colour" custom property is "Default", as set in the default view rep.

Is it possible to adjust it so that it gets the colour name assigned from the Colour view rep regardless of which view rep is active?

Another adjustment I am investigating is whether checking of the "Colour" view rep can be case insensitive? I have seen code for comparing strings and for converting all to the same case. Converting the View Rep name to lower case and then telling it to call the recursive function when the View rep name = colour seems like a good way to do this. What are your thoughts?

 

0 Likes
Message 10 of 21

tjvz85
Enthusiast
Enthusiast

Hi @Curtis_Waguespack ,

 

I am scouring the forum for code that will help automate our drawings process a little and it seems like you are involved in a lot of solutions. I hoping you can find the time to help me one more time.

 

Our Assembly .idw's will have at least 4 sheets named, e.g:

Page:1

Page:2

Partslist:1

Partslist:2

Page sheets are for illustrating the assemblies with their options or functions, the Partslist sheets hold the partslist, there will be more than one sheet if the partslist does not fit the A4 layout.

For the Partslist sheets I have a title block, different from the Page sheets, that I would like to have "Sheet # of #" automatically populate the set up fields, starting at "Sheet 1 of #" from sheet Partslist:1.

What I had in mind is for the code to check the sheet names and count the number of sheets called "Page" and use that amount for the below:

PartslistSheetsTotal = the actual Numbers of Sheets minus the Page sheets and

PartslistSheetNumber = the actual Sheet number minus Page sheets.

PartslistSheetsTotal would be a static value once all sheets are in the drawing file, but the variable for PartslistSheetNumber looks to be tricky.

 

I am trying to modify code from this post of yours https://forums.autodesk.com/t5/inventor-forum/changing-the-starting-sheet-number-in-an-idw-file/m-p/... along with what you posted above, but I am not getting the correct quantity of Page sheets. See my code below, it creates the custom iProperty, but the value is 0.

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSheets As Sheets
oSheets = ThisDoc.Document.Sheets
Dim oSheet As Sheet
oSheet = ThisDoc.Document.ActiveSheet
Dim oPageTotal As Integer
i = oPageTotal

'Iterate through the sheets
For Each oSheet In oSheets
	oSheet.Activate
		
	'Verify that the Sheet Name is Page
	If oSheet.Name = "Page" Then
		i = i+1
	End If
Next

iProperties.Value("Custom", "PartslistSheetTotal") = oPageTotal

I would appreciate any assistance to get past the first step and further. I am very uncertain on how to create the variable for PartslistSheetNumber.

 

0 Likes
Message 11 of 21

tjvz85
Enthusiast
Enthusiast

Hi @Curtis_Waguespack ,

 

I realise that the sheet name includes the sheet number, so I found this code to split the sheet name up to do the logic test, but I do not think I am applying it correctly. I am also not sure if the PageTotal is actually being added to and I am not calling it correctly.

	SheetName1 = ActiveSheet.Sheet.Name
          SheetName2 = Split(SheetName1, ":")(0)
0 Likes
Message 12 of 21

Curtis_Waguespack
Consultant
Consultant

Hi @tjvz85 

 

I'll try and look back at this later, but my first thought (after just a quick read through) is that we can right click on a sheet and choose "Edit Sheet" and then set the sheet not to be counted.

 

Is this something that would work for you?

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

aaa.png

EESignature

0 Likes
Message 13 of 21

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @tjvz85 

 

I had another quick look, and I think I would approach this as follows:

 

  • Edit the parts list sheets and exclude them from the sheet count as mentioned in my last post.
  • Set your parts list sheet title block to use a prompted entry called PartsList_OF_PartsLists
  • Then use the code below
  • Set this rule up as an external rule and have it trigger on the Before Save event trigger, so that if sheets are added or removed, the title block information gets updated on save

Note too that if you have more new questions it would best to start a new topic on the Inventor Customization forum so that these programming related questions are their own topic and are in the right place to accommodate future searches, which will help others find solutions in their searches.
http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/bd-p/120

 

 

aaa.png

 

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSheets As Sheets
oSheets = ThisDoc.Document.Sheets

Dim oSheet As Sheet
'Iterate through the sheets
i=0
For Each oSheet In oSheets		
	If oSheet.ExcludeFromCount = True Then
		i = i + 1
		oSheet.Name = "PartsList" & "-" & i		
	End If	
Next

oPartsListSheetCount = i

i = 0
For Each oSheet In oSheets	
	If oSheet.Name.Contains("PartsList") Then
		If oSheet.TitleBlock IsNot Nothing Then 
		    oTitleBlock = oSheet.TitleBlock
		    oTextBoxes = oTitleBlock.Definition.Sketch.TextBoxes
		    For Each oTextBox In oTitleBlock.Definition.Sketch.TextBoxes
				If oTextBox.Text = "PartsList_OF_PartsLists"
					i = i + 1
					oPromptEntry =  i & " OF " & oPartsListSheetCount
					Call oTitleBlock.SetPromptResultText(oTextBox, oPromptEntry)
					
				End If
		    Next
		End If
	End If
Next

 

 

EESignature

Message 14 of 21

tjvz85
Enthusiast
Enthusiast
Accepted solution

Thanks @Curtis_Waguespack ,

 

I adjusted your code a bit, but it would not have been possible without your input.

 

I didn't necessarily want the sheets to be renamed, as we have Sheet Formats setup with specific names. So my adjustment works soley on the name of the sheet. With the Exclude from Count approach, the creator of the idw will still need to do a work step to set sheet to be excluded. If it was possible to set sheets to be excluded automatically when being created from the Sheet Formats, that would have worked.

However, by creating two prompted entries in the titleblock and repeating the oPromptEntry step it was possible to achieve what I needed.

 

Please see my final code below. Is there a way to publish this part of the thread to the customization forum?

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSheets As Sheets
oSheets = ThisDoc.Document.Sheets

Dim oSheet As Sheet
'Iterate through the sheets
i=0
For Each oSheet In oSheets		
	If oSheet.Name.Contains("Stückliste") Then
		i = i + 1
	End If	
Next

oPartsListSheetCount = i

i = 0
For Each oSheet In oSheets	
	If oSheet.Name.Contains("Stückliste") Then
		If oSheet.TitleBlock IsNot Nothing Then 
		    oTitleBlock = oSheet.TitleBlock
		    oTextBoxes = oTitleBlock.Definition.Sketch.TextBoxes
		    For Each oTextBox In oTitleBlock.Definition.Sketch.TextBoxes
				If oTextBox.Text = "BlattAnzahl"
					oPromptEntry1 = oPartsListSheetCount
					Call oTitleBlock.SetPromptResultText(oTextBox, oPromptEntry1)
					
				End If
				If oTextBox.Text = "BlattNummer"
					i = i + 1
					oPromptEntry2 =  i
					Call oTitleBlock.SetPromptResultText(oTextBox, oPromptEntry2)
					
				End If
		    Next
		End If
	End If
Next

Browser & Titleblock filedsBrowser & Titleblock fileds

Thanks again for the help.

 
Message 15 of 21

Curtis_Waguespack
Consultant
Consultant

Hi @tjvz85 

 

It doesn't look like excluding a sheet from the sheet count can be done in a Sheet Format, but you can add a line to your rule, to have it exclude the partslist sheets, as in this example.

 

As for moving the post, I wouldn't worry about it, just keep the Customization forum in mind for the future.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSheets As Sheets
oSheets = ThisDoc.Document.Sheets

Dim oSheet As Sheet
'Iterate through the sheets
i=0
For Each oSheet In oSheets		
	If oSheet.Name.Contains("Stückliste") Then
		oSheet.ExcludeFromCount = True
		i = i + 1
	End If	
Next

'.... the rest of your code here

EESignature

Message 16 of 21

tjvz85
Enthusiast
Enthusiast

Excellent, thank you so much

0 Likes
Message 17 of 21

Dopey1993
Enthusiast
Enthusiast

this rule works great, however I would like to only select certain parts based on a certain color otherwise it set the line to the default.

 

like for instance it only changes the part that are the color blue otherwise don't change the line color at all.

0 Likes
Message 18 of 21

anthony.ravello
Enthusiast
Enthusiast

ey cutis
Nice Code. It works nice but i have the following problems:

- the Code doesnt work if i have a suppressed Part in my Assembly.

- I would like to change the line colours of the views but only for the active Sheet. Not all the Sheets.

 

Is it possible?

0 Likes
Message 19 of 21

glotz_99
Enthusiast
Enthusiast

Hey Curtis, thanks, this rule is exactly what I was looking for, but for some reason it only created one layer (for the cast iron colour) in autocad and all of the other parts stayed the same black and white. Is there something in the code I can change to fix this? Any help is greatly appreciated.  Also I noticed someone else said the code doesn't work if there are suppressed parts in the assembly, which I do have. 

0 Likes
Message 20 of 21

nguyen.np
Participant
Participant

Hi Curtis Waguespack

I'm in 2022 and looking for something like your code.

I try to create new Ilogic Rule and paste all the code and run it.

But it didn't work.

Are there something need to be setting before run it ??

Hope for your help.

0 Likes