Create custom iproperties in derived component

Create custom iproperties in derived component

agrove72Y9W
Participant Participant
799 Views
9 Replies
Message 1 of 10

Create custom iproperties in derived component

agrove72Y9W
Participant
Participant

Hello all, I have some code for creating and naming a derived component, but I would also like to create a few custom iproperties in that derived component before saving.

 

Here is the portion of code that does the deriving and saving. 

 

' create a new part file to derive the selected part into 
' note: kPartDocumentObject is the default template
Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject)) 

' create a derived definition for the selected part
Dim oDerivedPartDef As DerivedPartUniformScaleDef
oDerivedPartDef = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.CreateUniformScaleDef(oNewFileName & "-R" & RevString & ".ipt")

' set the scale to use
oDerivedPartDef.ScaleFactor = 1

' define the mirror plane
'XY plane = 27393 
'YZ plane = 27394 
'XZ plane = 27395 
oDerivedPartDef.MirrorPlane = 27394 

' create the derived part
oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Add(oDerivedPartDef)

' save the new derived part
oPartDoc.SaveAs(oNewFileName & "-L" & RevString & ".ipt", False)

oPartDoc.Close

 

I have already put the same custom iproperties into the parent part file and I'd like to put the exact same ones into the derived part, so it seems like it should be easy... Thanks in advance!

 

 

0 Likes
Accepted solutions (1)
800 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor

Hi @agrove72Y9W.  Just to clarify what you are requesting...are you wanting to create these custom iProperties within the new part that you are creating in line 4 of the code you just posted, or are you wanting to create these custom iProperties within the DerivedPartComponent that you created in line 20 of that code?  And you mentioned a 'parent' file already having these properties.  Is that parent the Template you used to create the new part from in line 4, or the source part that you derived into this new part?  Do you want to 'copy' those properties (with the same values) from that 'parent', or create new custom properties with the same names, but with different values?

If you are talking about accessing the new part that gets created after the SaveAs line, then capture that full file name you are using within that SaveAs line to a variable, then use ThisApplication.Documents.Open() with that full file name to make sure you have a reference to that newly created file.  Then you can access its iProperties after the SaveAs method.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 10

agrove72Y9W
Participant
Participant

I'm want the custom iProperties to be in the new DerivedPartComponent created in line 20.

 

I'm making left/right pairs of formed sheet metal parts. The right-hand part is the parent, and I am creating the left-hand part by mirroring about the X-Y plane. I want the left-hand part to have the same three custom iProperties that I have already created in the right-hand part, some with different values. For example, one of the custom iProperties is "Part_Number" which includes a -R or -L suffix to denote left and right-handedness.

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor

Hi @agrove72Y9W.  I'm actually not sure that would be possible.  I don't think multiple instances DerivedPartComponents within a part, which are all from the same source part can have their own independent properties.  Its pretty much just copied geometry and possibly derived in Parameters, instead of a Document after it has been derived in that way.  And iProperties are stored right under the Document object.  You might be able to use Attributes instead of iProperties for some things, but I'm not sure how you intend to use those properties later.  If these were ComponentOccurrences in an assembly, then they could have their own instance properties differently from each other.  I do use a lot of parts with other parts derived into them, but I honestly do not do a lot of code interactions with the derived in parts after the fact.  I know how to access the source file of the derived in part from the part that is has been derived into, but not the other way around, because the 'other' part does not know it has been derived into another part.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 10

A.Acheson
Mentor
Mentor

Hi @agrove72Y9W 

You will need to use the property sets to access the properties of the newly created part file. 

See below code.

	Dim oDoc As PartDocument = ThisDoc.Document
	Dim sPath As String = ThisDoc.Path
	Dim sFileName As String = ThisDoc.FileName(False)
	Dim sPartNo As String = iProperties.Value("Project", "Part Number")

	' create a new part file to derive the selected part into 
	' note: kPartDocumentObject is the default template
	Dim oPartDoc As PartDocument
	oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject)) 

	' create a derived definition for the selected part
	Dim oDerivedPartDef As DerivedPartUniformScaleDef
	oDerivedPartDef = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.CreateUniformScaleDef(oDoc.FullFileName)

	' set the scale to use
	oDerivedPartDef.ScaleFactor = 1

	' define the mirror plane
	'XY plane = 27393 
	'YZ plane = 27394 
	'XZ plane = 27395 
	oDerivedPartDef.MirrorPlane = 27394 

	' create the derived part
	oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Add(oDerivedPartDef)
	Try
		oPartDoc.PropertySets("Design Tracking Properties").Item("Part Number").Value = sPartNo
	Catch
	End Try
	
	' save the new derived part.
	Dim sNewFileName As String = sPath & "\" & sFileName & " DerivedPart.ipt"
	oPartDoc.SaveAs(sNewFileName, False)
	
	'oPartDoc.Close

 

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

agrove72Y9W
Participant
Participant

I tried to keep this simple, but it looks like I need to fully explain what I'm doing and where I want to be.

 

I am creating a library of standard parts for a new product line. Some of those parts have are left/right-handed, so I am treating the right-handed part as the parent, then making a separate left-handed part that is a mirror of the right-handed part. All of the parts in this library need to have three custom iProperties ("Part_Number", "Part_Description", and "Part_Reference") that will be called up in the material lists on our drawings. 

 

agrove72Y9W_0-1684783919930.png

 

There are quite a few different permutations of each part because their final shape/size depends on several variables, so I have created a rule that goes through each possible combination of those variables, and saves a file for each one. This allows me to quickly make changes to all of the instances within an entire family of parts as I build up the library. It will also allow for quick wholesale changes if we need to do so for all instances in the family. In this rule, I am iterating through each valid combination of variables, entering values for the three custom iProperties, updating the model, saving the right-handed version to a new filename, then creating the left-handed version. 

Below is the entirety of the rule that goes through this process.

Imports System
Imports System.Diagnostics
Imports System.Threading

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument

' total number of permutations for this part
Dim Iterations As Integer = 136

oQuestion = MessageBox.Show("Do you want to iterate through all " & Iterations & " permutations and save new files now?", "iLogic", MessageBoxButtons.YesNo)
If oQuestion = vbNo Then
Return
Else

' add revision string
RevString = InputBox("Do you want to add a revision suffix to the end of the part file? (example: -Rev1). If none is required, hit Cancel", "Revision Suffix", "")

' create list of all values for each variable
Dim oType As New ArrayList
Dim oTypeString As New ArrayList
Dim oLength As New ArrayList
Dim oPulleyDia As New ArrayList
Dim oSidewallHeight As New ArrayList

oType.Add("S")
oType.Add("I")

oTypeString.Add("Slider")
oTypeString.Add("Idler")

oLength.Add(5)
oLength.Add(6)
oLength.Add(7)
oLength.Add(8)
oLength.Add(9)
oLength.Add(10)
oLength.Add(11)
oLength.Add(12)

oPulleyDia.Add(10)
oPulleyDia.Add(12)
oPulleyDia.Add(14)
oPulleyDia.Add(16)

oSidewallHeight.Add(3)
oSidewallHeight.Add(6)
oSidewallHeight.Add(8)
oSidewallHeight.Add(10)


Dim oBaseFilePath As String = ThisDoc.Path & "\"
Dim oPNstring As String
Dim oNewFileName As String

For a As Integer = 0 To oType.Count() - 1
	Parameter("Type") = oTypeString(a)

	If Parameter("Type") = "Slider"
		cMIN = 0
		cMAX = 3
		dMIN = 0
		dMAX = 1
	Else If Parameter("Type") = "Idler"
		cMIN = 1
		cMAX = 3
		dMIN = 1
		dMAX = 3
	End If

	For b As Integer = 0 To oLength.Count() - 1
		Parameter("Length") = oLength(b) * 12
		
		For c As Integer = cMIN To cMAX
			Parameter("Pulley_Diameter") = oPulleyDia(c)
			
			For d As Integer = dMIN To dMAX
				Parameter("Sidewall_Height") = oSidewallHeight(d)
			
				Dim StartTime As DateTime
				Dim ElapsedTime As TimeSpan
				Dim ProjectedTime As TimeSpan
				
				' start timer for one iteration
				If a = 0 And b = 0 And c = 0 And d = 0
					StartTime = Now
				End If
				
				' create part_number string
				oPNstring = "HP-" & oType(a) & "-" & oLength(b) & "-" & oPulleyDia(c) & "-" & oSidewallHeight(d)

				' create part_description string
				Width = Parameter("Length")
				Wfeet = Floor(Width / 12)
				Winches = Floor(Width - (Wfeet * 12))
				Wremainder = Width - Floor(Width)
	
				Height = Parameter("Pulley_Diameter") + Parameter("Sidewall_Height") + 6.5 + 5.2297842
				Hfeet = Floor(Height / 12)
				Hinches = Floor(Height - (Hfeet * 12))
				Hremainder = Height - Floor(Height)
				
				' write to custom properties
				iProperties.Value("Custom", "Part_Number") = oPNstring
				
				If Width < Height Then
					WidthDesc = RoundToFraction(Width, 1 / 16, RoundingMethod.Round)
					If Hremainder = 0
						HeightDesc = String.Format("{0}'", Hfeet) & "-" & String.Format("{0}", Hinches)
					Else
						HeightDesc = String.Format("{0}'", Hfeet) & "-" & String.Format("{0}", Hinches) & " " & RoundToFraction(Hremainder, 1 / 16, RoundingMethod.Round)
					End If
					iProperties.Value("Custom", "Part_Description") = "3/16 X " & WidthDesc & " X " & HeightDesc
				Else	
					HeightDesc = RoundToFraction(Height, 1 / 16, RoundingMethod.Round)
					If Wremainder = 0
						WidthDesc = String.Format("{0}'", Wfeet) & "-" & String.Format("{0}", Winches)
					Else
						WidthDesc = String.Format("{0}'", Wfeet) & "-" & String.Format("{0}", Winches) & " " & RoundToFraction(Wremainder, 1 / 16, RoundingMethod.Round)
					End If
					iProperties.Value("Custom", "Part_Description") = "3/16 X " & HeightDesc & " X " & WidthDesc
				End If
				
				iProperties.Value("Custom", "Part_Reference") = "HEAD PANEL"
				
			
				' create new filename
				oNewFileName = 	oBaseFilePath & oPNstring
				
				' update model before save as
				iLogicVb.RunRule("Dim_Rule")
				InventorVb.DocumentUpdate()

				' save as new file
				ThisDoc.Document.SaveAs(oNewFileName & "-R" & RevString & ".ipt", True)			

				' create a new part file to derive the selected part into 
				' note: kPartDocumentObject is the default template
				Dim oPartDoc As PartDocument
				oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject)) 

				' create a derived definition for the selected part
				Dim oDerivedPartDef As DerivedPartUniformScaleDef
				oDerivedPartDef = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.CreateUniformScaleDef(oNewFileName & "-R" & RevString & ".ipt")

				' set the scale to use
				oDerivedPartDef.ScaleFactor = 1

				' define the mirror plane
				'XY plane = 27393 
				'YZ plane = 27394 
				'XZ plane = 27395 
				oDerivedPartDef.MirrorPlane = 27394 

				' create the derived part
				oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Add(oDerivedPartDef)

				' save the new derived part
				oPartDoc.SaveAs(oNewFileName & "-L" & RevString & ".ipt", False)
				oPartDoc.Close
				'MessageBox.Show("New file created at:" & vbLf & oNewFileName & "-L.ipt", "iLogic")
				
				If a = 0 And b = 0 And c = 0 And d = 0
					' get the elapsed time for one iteration
					ElapsedTime = Now().Subtract(StartTime)
					
					' calculate total time to do all iterations
					For i As Integer = 1 To Iterations
						ProjectedTime = ProjectedTime.Add(ElapsedTime)
					Next
					
					' format and display the TimeSpan value.
					Dim TimeString As String = String.Format("{0:00}m:{1:00}s", ProjectedTime.Minutes, ProjectedTime.Seconds)
					
					' calculate total time this will take based on oNumPermutations
					oQuestion = MessageBox.Show("This operation will take about " & TimeString & " Do you want to continue?", "iLogic",MessageBoxButtons.YesNo)
					If oQuestion = vbNo Then
						Return
					Else
					End If
				End If
			Next
		Next
	Next
Next

End If

 

My goal is to have the left-handed part have the same three custom iProperties with only one change - the '-R' at the end of the "Part_Number" will be '-L' in the mirrored part. The other two iProperties will be identical for both right and left-handed instances.

0 Likes
Message 7 of 10

A.Acheson
Mentor
Mentor
Accepted solution

Hi @agrove72Y9W 

 

Before you save the derived part you just need to create a custom property(iproperty) and then set it's value. You can pick up the value from the previous iproperty and pass in it's value. See previous example. 

	Try
		oPartDoc.PropertySets("Inventor User Defined Properties").Item("Part_Description").Value = "12345"
	Catch
oPartDoc.PropertySets("Inventor User Defined Properties").Add("12345", "Part_Description")
End Try
' save the new derived part
oPartDoc.SaveAs(oNewFileName & "-L" & RevString & ".ipt", False)

 

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

WCrihfield
Mentor
Mentor

OK, now I understand the situation much better.  That clarified what was needed to achieve your goals, as Alan has suggested above.  However, I think I would have preferred to simply copy the right hand part's file to create the left hand part file, then edited that one property value in the copied file.  I do understand that you are using the derive process to help ensure that the right and left hand parts stay identical when changes are made, but then you have to create all 3 custom iProperties in that other part, instead of just editing the one.  I'm guessing you could also have linked the parameters of the one part directly to the parameters of the other part too, if you wanted to do it that way.  It seems like the derive process would also unnecessarily complicate assembly constraint replacements later on, if you were to replace one part for the other, because the two models no longer share identical ancestry (Face 1 of the right hand part and Face 1 of the left hand part would not have the same internal ID anymore).  Just some thoughts.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 10

agrove72Y9W
Participant
Participant

Thank you very much, Alan! I knew it had to be something simple like this.

 

Thank you all for your assistance

0 Likes
Message 10 of 10

A.Acheson
Mentor
Mentor

Hi @agrove72Y9W 

Yes this task was virtually impossible without knowing the API method for accessing iproperties. See this helpful article on accessing them yourself.

 

I tried the ilogic method using the short one liner but I could not target the derived document object.

 

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