Copy iProperties from .iam to all .ipt-iLogic

Copy iProperties from .iam to all .ipt-iLogic

Christy.Tuan
Participant Participant
1,168 Views
6 Replies
Message 1 of 7

Copy iProperties from .iam to all .ipt-iLogic

Christy.Tuan
Participant
Participant

Hi everyone,

I hope someone can help me with this, because i have no idea about Programming...I use multi-bodies to creat .iam, that's why all the .ipt have no iProperties. So My purpose is to copy some iProperties from .iam to ALL the .ipt. And then open ALL the .ipt. 

I creat a Form to give all info, then i can click the rule to run the program.

 

JiayingDuan_0-1631634327073.png

I don't know how to code, so i googled all the codes that i can copy. But, obviously it won't work when i put them together, don't laugh!... i will show them below as a reference. Thanks a million to whom provide the help ^^

 

P.s. 1. the iProperties as "No. Contrat" or "Coordonnateur Technique" have "." and space. I don't know how to code them into the program.

      2.  the phrase:  it opens all the .ipt, inclues the multy-body.ipt. I dont want to open it and give all iproperties to it. But if it's too complicated to take it out from the programming. I can keep it.

 

ThisApplication.Documents.Open(oDoc.FullFileName, True) 'True is visible, false for invisible

 

My codes:

 

Sub Main()
	
Dim oRefName As String 
 
If ThisDoc.Document.DocumentType = kAssemblyDocumentObject Then
	 
'Define assembly document
	Dim oAsmDoc As AssemblyDocument
	
	oAsmDoc = ThisDoc.Document
	
	Dim oAsmDef As AssemblyComponentDefinition
	
	oAsmDef = oAsmDoc.ComponentDefinition
	
	Dim oAsmRef As ComponentOccurrence
	
'Pick the first item
	oAsmRef = oAsmDef.Occurrences.Item(1)
	oRefName = oAsmRef.Name

    Return
End If

'Copie les iPropriétés

iProperties.Value("Project", "Project") = _
iProperties.Value(oRefName, "Project", "Project")

iProperties.Value("Project", "Designer") = _
iProperties.Value(oRefName, "Project", "Designer")


'Open all the parts
Dim oDoc As Document

For Each oDoc In ThisApplication.ActiveDocument.AllReferencedDocuments

ThisApplication.Documents.Open(oDoc.FullFileName, True) 'True is visible, false for invisible

Next

End Sub

 

 

 

0 Likes
Accepted solutions (1)
1,169 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @Christy.Tuan.  I think I may have something you can use for your iLogic rule.  Copy the contents of the code below into your existing iLogic rule, to replace what you currently have in it.  It starts out by making sure that the 'active' document is an assembly, then if so captures it.  Then gets its component definition.  Then I create a list to keep track of the documents it will be processing, to ensure we don't try to process the same ones more than once.  Then it starts 'looping' through all the part type components in the assembly.  Checks if they are in the list yet.  If so, skip to next component.  Then it tries to copy assembly iProperty values to part iProperties.  Then visibly opens the document.  Then adds the component document's full file path to the list, to record it.  Some of this is within a Try...Catch...End Try block of code, so that if it fails for some reason, it won't stop the rule from progressing to the next item, while still letting you know about the failure.

Here is the code:

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oList As List(Of String) = New List(Of String)
For Each oComp As ComponentOccurrence In oADef.Occurrences.AllLeafOccurrences
	If oList.Contains(oComp.Definition.Document.FullFileName) Then Continue For
	Try
		iProperties.Value(oComp.Name, "Project", "Project") = iProperties.Value("Project", "Project")
		iProperties.Value(oComp.Name, "Project", "Designer") = iProperties.Value("Project", "Designer")
		'iProperties.Value(oComp.Name, "Custom", "No. Contrat") = iProperties.Value("Project", "No. Contrat")
		'iProperties.Value(oComp.Name, "Custom", "Coordonnateur Technique") = iProperties.Value("Project", "Coordonnateur Technique")
		ThisApplication.Documents.Open(oComp.Definition.Document, True)
	Catch e As Exception
		MsgBox("Failed to copy some assembly iProperties to part." & vbCrLf & _
		e.Message & vbCrLf & e.StackTrace,vbInformation,"iLogic")
	End Try
	oList.Add(oComp.Definition.Document.FullFileName)
Next

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7

Christy.Tuan
Participant
Participant

Thanks to help me @WCrihfield !!! I replaced my code by yours. There is a problem shows repeatly for each .ipt. And the .ipt doesn't open yet.

JiayingDuan_0-1631641431311.png

 

I checked each .ipt, the iProperties of "Project" and "Designer" are added, but "No. Contrat"or"Coordonnateur technique". Even I delete apostrophe ' in front of the phrase:

	'iProperties.Value(oComp.Name, "Custom", "No. Contrat") = iProperties.Value("Project", "No. Contrat")

 

Do you think it's because these Custom iProperties should be created in each .ipt before we copy them from .iam?

 

JiayingDuan_1-1631641731143.png

Thanks!

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor

Well, I see one mistake I made in my code right away.  My line of code to open the document, is specifying the document object directly as input, instead of its FullFileName.  That will definitely cause an error.  And when one action within the Try part of a Try...Catch block of code fails, it automatically undoes all actions performed within that Try portion, so if any fail, they all fail.

 

You could also be right about having to create the iProperties in those parts.  I assumed that those last two items in your Form represented 'custom' iProperties.  I guess I was thinking that the iProperties.Value() snippet would automatically create the 'custom' iProperties if they weren't found in the parts, but I might be wrong, because I obviously haven't tested this specific rule myself yet.    I could try doing this the 'long way' to help avoid the potential errors if necessary, but try the first fix mentioned above first.

(long way = Document.PropertySets.PropertySet.Property.Value = (other document's iProperty value))

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 7

WCrihfield
Mentor
Mentor

@Christy.Tuan 

I also see that I mistakenly put "Project" instead of "Custom" as the iProperty set name in the last part of the two lines of code for the two custom iProperties.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 7

WCrihfield
Mentor
Mentor
Accepted solution

@Christy.Tuan

Sorry about the earlier mistakes.  I rewrote the rule differently, without using a Try...Catch block of code, and included code to create the custom iProperties if they aren't found.  It's quite a bit longer now due to doing some stuff the longer way.  I included lots of comments about what the following line or block of code is about to do, throughout the code, to help you understand and follow along.

Here is the new rule code:

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition

'record assembly iProperty values to variables
Dim oAProj As String = iProperties.Value("Project", "Project")
Dim oADes As String = iProperties.Value("Project", "Designer")
Dim oAContrat As String = iProperties.Value("Custom", "No. Contrat")
Dim oACoord As String = iProperties.Value("Custom", "Coordonnateur Technique")

Dim oList As List(Of String) = New List(Of String)
For Each oComp As ComponentOccurrence In oADef.Occurrences.AllLeafOccurrences
	'get the document object that this component represents
	Dim oCompDoc As Document = oComp.Definition.Document
	
	'check if it's full file name is already within the list, if so, skip to next component
	If oList.Contains(oCompDoc.FullFileName) Then Continue For
	
	'get this component's 'Project' iProperty Set
	Dim oProjProps As PropertySet = oCompDoc.PropertySets.Item("Design Tracking Properties")
	
	'loop through them and set these 2 property values
	For Each oProjProp As Inventor.Property In oProjProps
		If oProjProp.Name = "Project" Then
			oProjProp.Value = oAProj
		ElseIf oProjProp.Name = "Designer" Then
			oProjProp.Value = oADes
		End If
	Next
	
	'get this component's 'Custom' iProperty Set
	Dim oCProps As PropertySet = oCompDoc.PropertySets.Item("Inventor User Defined Properties")
	
	'create variables for the two 'custom' iProperties we're attempting to access
	Dim oContrat, oCoord As Inventor.Property
	
	'try to find these two custom iProperties within the set
	For Each oCProp As Inventor.Property In oCProps
		If oCProp.Name = "No. Contrat" Then
			oContrat = oCProp
		ElseIf oCProp.Name = "Coordonnateur Technique" Then
			oCoord = oCProp
		End If
	Next
	
	'check if we found this one, if so, set its value, if not, create it
	If Not IsNothing(oContrat) Then
		oContrat.Value = oAContrat
	Else
		oContrat = oCProps.Add(oAContrat, "No. Contrat")
	End If
	
	'check if we found this one, if so, set its value, if not, create it
	If Not IsNothing(oCoord) Then
		oCoord.Value = oACoord
	Else
		oCoord = oCProps.Add(oACoord, "Coordonnateur Technique")
	End If
	
	'open this component's document
	oOpenDoc = ThisApplication.Documents.Open(oCompDoc.FullFileName, True)
	
	'write this component document's full file name to the list
	oList.Add(oCompDoc.FullFileName)
Next

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 7

Christy.Tuan
Participant
Participant

Hi @WCrihfield Sorry to reply you late. I run your latest code. it opens all the .ipt but still no iProperties of No. Contrat or Coordonnateur Technique. But i understand what you mentioned the mistakes in the first code. So I copied some phrases of the second to the first and it works peferctly. 

The code: 

iProperties.Value(oComp.Name, "Custom", "No. Contrat") = iProperties.Value("Custom", "No. Contrat")

can create the Custom iproperties. Thank you so much 😀!!!!!

 

In case you or someone want to see or copy it, i put all code here:

Sub Main()
	
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oList As List(Of String) = New List(Of String)
For Each oComp As ComponentOccurrence In oADef.Occurrences.AllLeafOccurrences
	
	Dim oCompDoc As Document = oComp.Definition.Document
	
	If oList.Contains(oComp.Definition.Document.FullFileName) Then Continue For
	Try
		iProperties.Value(oComp.Name, "Project", "Project") = iProperties.Value("Project", "Project")
		iProperties.Value(oComp.Name, "Project", "Designer") = iProperties.Value("Project", "Designer")
		iProperties.Value(oComp.Name, "Custom", "No. Contrat") = iProperties.Value("Custom", "No. Contrat")
		iProperties.Value(oComp.Name, "Custom", "Coordonnateur Technique") = iProperties.Value("Custom", "Coordonnateur Technique")
	
	'open this component's document
	oOpenDoc = ThisApplication.Documents.Open(oCompDoc.FullFileName, True)
	
	Catch e As Exception
		MsgBox("Failed to copy some assembly iProperties to part." & vbCrLf & _
		e.Message & vbCrLf & e.StackTrace,vbInformation,"iLogic")
	End Try
	oList.Add(oComp.Definition.Document.FullFileName)
	
Next

End Sub

 

0 Likes