Modify iProperty of every part in an assembly

Modify iProperty of every part in an assembly

emanuel.c
Collaborator Collaborator
3,101 Views
8 Replies
Message 1 of 9

Modify iProperty of every part in an assembly

emanuel.c
Collaborator
Collaborator

I have this short piece of code. It fills in the Description iProperty with the part's browser name in model tree. It works great for all parts and subassemblies found at the top level of an assembly, but it stops there. How can I have it work on ALL parts including parts in sub and sub-sub assemblies etc.?

Many thanks!

'Copy file name (browser name) to description for each occurence in an assembly and subassembly

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Iterate through all of the occurrences in the assembly
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
    Dim oName As String
    oName = oOccurrence.Name
	'split browser name between name and sequence number (what follows after ":") 
	ONameArray = Split(oName, ":")
	oName=ONameArray(o)	
    'Fill in part Description from File Name
	iProperties.Value(oOccurrence.Name, "Project", "Description") = oName
Next

 

0 Likes
Accepted solutions (1)
3,102 Views
8 Replies
Replies (8)
Message 2 of 9

A.Acheson
Mentor
Mentor

You can use the all referenced document method which would be the shortest as it works with the document itself rather than the occurrences which can be many. 

The alternative is Traverse Assembly through each sub assembly. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 9

WCrihfield
Mentor
Mentor

Writing assembly level data back down into sub-assembly & part level documents is often a bad idea.  There are several situations where the data may be written over multiple times, which can result in incorrect & misleading information without it being obvious that there is a problem.  Especially in situations where the same document may be represented by components in multiple other main assemblies, or the same document may be represented by components in multiple levels of the same main assembly structure.  What do you plan on doing with this 'component name' information in the description iProperty, once the task of writing the data out is done?  If there are multiple components that represent the same document, which component name should be written to the document?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 9

emanuel.c
Collaborator
Collaborator

@WCrihfield yes, here is what I'm looking for. In our work we mainly name a part's File Name descriptively (no particular part number, but rather, for example "mounting bracket" or "main frame tube 1" etc. though frame generated parts I let be). This makes it easier for the designer to find an assembly or part in a Windows folder. Instead of being part "102-405" it's "main frame tube 1". 

 

With this code I would copy the file name (it worked better to copy the part name from Inventor's model tree, but now I forget why that was) to the part's Description iProperty. I typically run this code once, just before creating the technical / shop drawings and probably not again. If I need to tweak a part's Description, I do that leaving the part's file name as it was - so as not to have to deal with further issues there. In the Parts List of a dwg/dxf drawing each part gets its Part Name as it is given in the BOM. So the File Name is not the same as the Part Name, but the description may very well be what the File Name is. 

 

For some reason I think you had mentioned the potential issues before but I've forgotten about that. Yes, there could very well be subassemblies and parts found in multiple subassemblies.

 

Does this help? Would you suggest I stick with what I have and simply run the code as I have it in each subassembly?

 

Thank you much!

0 Likes
Message 5 of 9

WCrihfield
Mentor
Mentor

You know your file system, files, and documentation routine a whole lot better than I do, so I would not venture to suggest which way would work best for you in the end.  I just wanted to remind you of a few potentially problematic scenarios that could arise in that situation, and suggest caution.  I don't know how your system currently names your components in your assemblies, or if you use a routine or tool to rename all of the components after the fact or not, to stabilize them.  My system names the components using the file name, without the file extension, then automatically adds the ":1", ":2", and so on after the name, to keep them unique.  If yours is the same way, and you have multiple components in your main assembly's top level all representing one document, then some components within your sub assemblies representing that same document, and you did not rename any of your components in any way, then I suppose the end result of running your code or another similar one on all levels of the assembly would simply result in the document's iProperty being written to multiple times, but always with the same name, so do big deal either way.  The iProperty would still just contain the file name, without the file extension.  Seems like a routine that could be done on a per-document basis just as easily as through an assembly structure, if that information is always exactly what you need.  I would be more tempted to get the information from the document's file name though, instead of from the component name, but that's my personal preference.

If you need further help writing a different variation of your code, I'm sure we can help with that.  When working my way through a multi-level assembly structure though, I would suggest using the API way of accessing the iProperties, instead of using that iProperties.Value() snippet.  Again...personal preference.  One think to think about is, what if the same component name exists in multiple places throughout the assembly's levels.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 9

emanuel.c
Collaborator
Collaborator

Hi @WCrihfield Yes, my file naming is same as yours. And I suppose it wouldn't be a big deal if the Description iProperty would be written multiple times if the part or subassembly is found multiple times throughout the assembly, as in different subassemblies.

 

I wouldn't necessarily fill in Description on a per document basis. I would do it one time from the top assembly level and then modify it if needed. (Sometimes it can be a bother to change the file name and replace the part.) 

 

I don't mind if the Description is pulled from the file name or the component name. Both would work, but at that point with whatever help I had that worked well (which it may have been @A.Acheson who helped). But after a while of digging through the subassemblies to run the code, I thought it be great to have it run from the top level assembly down to all subassemblies and parts included.

 

So yes, if you can help I'd be very grateful. I just didn't know how to make the For loop run through all the subassemblies and parts. I'm still try the "AllReferencedDocuments" which @A.Acheson mentioned.

0 Likes
Message 7 of 9

WCrihfield
Mentor
Mentor
Accepted solution

We can stick with the AllReferencedDocuments route, if we populate the Description iProperty from the Document's file name.  Here is a fairly simple example iLogic code that will do that task.  I separated the main process out into its own Sub routine, and I am also using a Try...Catch block within that to help handle/avoid the potential errors.  It is sometimes possible that not every referenced document is a regular Inventor Part or Assembly document.

Sub Main
	oDoc = ThisDoc.Document
	WriteFileNameToDescription(oDoc)
	If oDoc.AllReferencedDocuments.Count = 0 Then Exit Sub
	For Each oRefDoc As Document In oDoc.AllReferencedDocuments
		WriteFileNameToDescription(oRefDoc)
	Next
End Sub

Sub WriteFileNameToDescription(oDoc As Document)
	Try
		'gets file name, without path, and without file extension
		oFileName = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
		oDescProp = oDoc.PropertySets.Item("Design Tracking Properties").Item("Description")
		If oDescProp.Value <> oFileName Then oDescProp.Value = oFileName
	Catch oEx As Exception
		MsgBox(oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "")
		'Logger.Error(oEx.Message & vbCrLf & oEx.StackTrace)
	End Try
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 9

emanuel.c
Collaborator
Collaborator

Well, it works perfectly (not surprising). Thank you for your help! And I do learn a little bit each time I venture out to try to write some code and get some help with it. Ilogic is beautiful.

0 Likes
Message 9 of 9

mu4573
Contributor
Contributor

hello. This code works fine for me too. But... can't we use the form to edit it?

0 Likes