iLogic to change iProperties of existing virtual component

iLogic to change iProperties of existing virtual component

jamie.lambJXK6F
Explorer Explorer
564 Views
5 Replies
Message 1 of 6

iLogic to change iProperties of existing virtual component

jamie.lambJXK6F
Explorer
Explorer

Hi, I'm trying to write some iLogic that will create a virtual component, and populate its Part Number and Description iProperties to match the parent assembly (the reason I want to do this is not important).  I'd like it to just populate these iProperties if that virtual component already exists. I'm a real novice at this, but have managed to get the following to work by copying snippets of code from elsewhere:

 

'Create Virtual Component
oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual("virtualPart", oMatrix)


'edit iProperties
oOcc.Definition.PropertySets.Item(3).Item("Part number").Value = iProperties.Value("Project", "Part Number")
oOcc.Definition.PropertySets.Item(3).Item("Description").Value = iProperties.Value("Project", "Description")

 

This works great to create the virtual part and populate the iProperties, but if I run it again, it throws an error ("The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))" ) and does not update the iProperties, presumably because "virtualPart" already exists.  How can I get this to update the iProperties if the virtual component has already been created?

 

Apologies for the simplistic nature of this, like I said, I'm very new to this!

0 Likes
Accepted solutions (1)
565 Views
5 Replies
Replies (5)
Message 2 of 6

A.Acheson
Mentor
Mentor

Hi @jamie.lambJXK6F 

You can use a Try Catch statement to catch the error of occurrence allready existing and then reference this occurrence using item by name see help page here

Try
'Create Virtual Component
   oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual("virtualPart", oMatrix)
Catch
   oOcc = oAsm.ComponentDefinition.Occurrences.ItemByName("virtualPart")
End Try

 

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 6

jamie.lambJXK6F
Explorer
Explorer

Hi  @A.Acheson,

 

Thanks for this. I see the idea, but unfortunately it still doesn't work.  My full code  is as follows:

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix

Dim oOcc As ComponentOccurrence


Try
'Create Virtual Component
   oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual("virtualPart", oMatrix)
Catch
   oOcc = oAsm.ComponentDefinition.Occurrences.ItemByName("virtualPart")
End Try


'edit iProperties
oOcc.Definition.PropertySets.Item(3).Item("Part number").Value = iProperties.Value("Project", "Part Number")
oOcc.Definition.PropertySets.Item(3).Item("Description").Value = iProperties.Value("Project", "Description")

 

As before, runing this works great if the virtual part has not yet been created, but if it is already existing, I get the error :'The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))".

 

I think the problem is in the line 

   oOcc = oAsm.ComponentDefinition.Occurrences.ItemByName("virtualPart")

 Because when I delete the step to create the virtual part altogether and just use this to try to set oOcc when I have already previously created the virtual component, I get the same error.

 

I've looked at the help page you linked, thanks for that, but I'm afraid it's a bit above my level of understanding... is there perhaps a simple syntax error I have wrong in that problem line?

0 Likes
Message 4 of 6

guillaume_aubin
Explorer
Explorer

Hi @jamie.lambJXK6F 

 

I tested you program and I have come to this conclusion:

You were right it was this line the problem.

oOcc = oAsm.ComponentDefinition.Occurrences.ItemByName("virtualPart")

The reason is that when added to the assembly, the name of the virtual part is not " virtualPart" as expected, but it become "virtualPart:1". You can verify this with a line like this  :

MessageBox.Show(oAsm.ComponentDefinition.Occurrences.Item(1).Name)

 (Note that the virtualPart must be the only occurence in the assembly to be Item(1) )

0 Likes
Message 5 of 6

J-Camper
Advisor
Advisor
Accepted solution

@jamie.lambJXK6F,

 

The DisplayName Can be checked, as that should be the name used to create the virtual part.  here is an example:

Sub Main
	Dim aDoc As AssemblyDocument = TryCast(ThisApplication.ActiveDocument, AssemblyDocument)
	If IsNothing(aDoc) Then Logger.Debug("Not Run In Assembly Document") : Exit Sub
		
	Dim virtComponentName As String = "virtualPart"
	Dim virtComponentPartNumber As Object = aDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
	Dim virtComponentDescription As Object = aDoc.PropertySets.Item("Design Tracking Properties").Item("Description").Value
	
	Dim virtComponent As ComponentOccurrence = Nothing
	
	For Each co As ComponentOccurrence In aDoc.ComponentDefinition.Occurrences.Cast(Of ComponentOccurrence).Where(Function(v) v.Definition.Type.Equals(ObjectTypeEnum.kVirtualComponentDefinitionObject))
		If co.Definition.DisplayName <> virtComponentName Then Continue For
		virtComponent = co
		Exit For
	Next
	
	If IsNothing(virtComponent)
		virtComponent = aDoc.ComponentDefinition.Occurrences.AddVirtual(virtComponentName, ThisApplication.TransientGeometry.CreateMatrix())
	End If
	
	virtComponent.Definition.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = virtComponentPartNumber
	virtComponent.Definition.PropertySets.Item("Design Tracking Properties").Item("Description").Value = virtComponentDescription
	
End Sub

 

Let me know if you have any questions

0 Likes
Message 6 of 6

jamie.lambJXK6F
Explorer
Explorer
Amazing, this works perfectly! Thanks @J-Camper
0 Likes