virtual component

virtual component

martinhoos
Advocate Advocate
2,872 Views
11 Replies
Message 1 of 12

virtual component

martinhoos
Advocate
Advocate

Hello together,

i created a code that tooks an virtual component from an excel file. The problem that i have is, if the component allready exists, i get an error. How can i fix that problem? If compnent exist, delete it and place an new one in... but i dont know, how.

Thank you in advance

Regards

Martin

 

Dim retVal
Dim Menge As String = "Menge"

GoExcel.Open("c:\iproperties\iproperties.xlsx", "data")

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

Dim oOcc As ComponentOccurrence
'oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual("EXAMPLE", oMatrix)
oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual(GoExcel.CellValue("Z2"), oMatrix)

Dim oCompDefVirtual As VirtualComponentDefinition = oOcc.Definition

'CREATE AND SET THE IPROP
Dim oPropertySet As PropertySet = oCompDefVirtual.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
iProperties.Value(GoExcel.CellValue("Z2") & ":1","Summary", "Title")=GoExcel.CellValue("AA2")
iProperties.Value(GoExcel.CellValue("Z2") & ":1","Summary", "Subject")=GoExcel.CellValue("BB2")
iProperties.Value(GoExcel.CellValue("Z2") & ":1","Summary", "Author")=GoExcel.CellValue("Z2")
iProperties.Value(GoExcel.CellValue("Z2") & ":1","Summary", "Manager")=GoExcel.CellValue("AF2")
iProperties.Value(GoExcel.CellValue("Z2") & ":1","Project", "Description")=GoExcel.CellValue("AA2") + "  " + GoExcel.CellValue("BB2")

customPropertySet = ThisDoc.Document.PropertySets.Item _
("Inventor User Defined Properties")
Try
         prop= customPropertySet.Item(Menge)
Catch
            customPropertySet.Add("", Menge)
End Try
Menge = InputBox("Bitte die Menge eingeben, z.B. 2,5", "iLogic", iProperties.Value("Custom", "Menge"))
0 Likes
Accepted solutions (1)
2,873 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable

You can use Try - Catch method also when you are creating the virtual component. 

 

 

0 Likes
Message 3 of 12

martinhoos
Advocate
Advocate

Thanks RAS,

sounds good - but i dont know how. I do not have so much experience in iLogic. Can you tell me how to do that?

Regards

Martin

0 Likes
Message 4 of 12

Anonymous
Not applicable

Here is how it should work(I didn't test):

 

Dim oOcc As ComponentOccurrence
Try
       oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual(GoExcel.CellValue("Z2"), oMatrix)
Catch ex As Exception
      For Each oOcc In oAsm.ComponentDefinition.Occurrences
          If oOcc.Name = GoExcel.CellValue("Z2") Then
               oOcc.Delete()
          End If
      Next
oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual("Name of component", oMatrix)


End Try

0 Likes
Message 5 of 12

martinhoos
Advocate
Advocate
Dim retVal
Dim Menge As String = "Menge"

GoExcel.Open("c:\iproperties\iproperties.xlsx", "data")

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



Dim oOcc As ComponentOccurrence
Try
oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual(GoExcel.CellValue("Z2"), oMatrix)
Catch ex As Exception
      For Each oOcc In oAsm.ComponentDefinition.Occurrences
          If oOcc.Name = GoExcel.CellValue("Z2") Then
               oOcc.Delete()
          End If
      Next

oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual(GoExcel.CellValue("Z2"), oMatrix)

End Try


Dim oCompDefVirtual As VirtualComponentDefinition = oOcc.Definition

'CREATE AND SET THE IPROP
Dim oPropertySet As PropertySet = oCompDefVirtual.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
iProperties.Value(GoExcel.CellValue("Z2") & ":1","Summary", "Title")=GoExcel.CellValue("AA2")
iProperties.Value(GoExcel.CellValue("Z2") & ":1","Summary", "Subject")=GoExcel.CellValue("BB2")
iProperties.Value(GoExcel.CellValue("Z2") & ":1","Summary", "Author")=GoExcel.CellValue("Z2")
iProperties.Value(GoExcel.CellValue("Z2") & ":1","Summary", "Manager")=GoExcel.CellValue("AF2")
iProperties.Value(GoExcel.CellValue("Z2") & ":1","Project", "Description")=GoExcel.CellValue("AA2") + "  " + GoExcel.CellValue("BB2")

customPropertySet = ThisDoc.Document.PropertySets.Item _
("Inventor User Defined Properties")
Try
         prop= customPropertySet.Item(Menge)
Catch
            customPropertySet.Add("", Menge)
End Try
Menge = InputBox("Bitte die Menge eingeben, z.B. 2,5", "iLogic", iProperties.Value("Custom", "Menge"))

Hello RAS,

 

thank you for responce... but there is a mistake in the code, i get this message (enclose).

 

The code i used:

0 Likes
Message 6 of 12

Curtis_Waguespack
Consultant
Consultant

Hi martinhoos,

 

This example shows how to delete existing instances and replace them:

http://inventortrenches.blogspot.com/2013/07/ilogic-add-standard-virtual-parts-from_29.html

 

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

 

EESignature

0 Likes
Message 7 of 12

Anonymous
Not applicable

Yes there was a bug in my code. Occurrence name is not equal than part number, occurrence name has ":1" in the end of the name. But we can compare the displayname and this should be the same than the name given when component is created. 

 

There seems to be good sample in Cutris's blog also. The idea seems to be the same.

 

Try this:

 

Dim oOcc As ComponentOccurrence
Try
       oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual(GoExcel.CellValue("Z2"), oMatrix)
Catch ex As Exception
      For Each oOcc In oAsm.ComponentDefinition.Occurrences
          If oOcc.Definition.Displayname = GoExcel.CellValue("Z2") Then
               oOcc.Delete()
          End If
      Next
oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual(GoExcel.CellValue("Z2") , oMatrix)


End Try

0 Likes
Message 8 of 12

martinhoos
Advocate
Advocate

Thank you Curtis and RAS,

i gambled a couple of hours with both ideas - but i get no result, it is too hard for me. I took the code from RAS into my code. It works only in an empty assembly. If there are parts in, i get following message:

 

 

System.MissingMemberException: Der öffentliche Member Displayname für den Typ SheetMetalComponentDefinition wurde nicht gefunden.

bei Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)

bei LmiRuleScript.Main()

bei Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)

bei iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

or

 

System.MissingMemberException: Der öffentliche Member Displayname für den Typ PartComponentDefinition wurde nicht gefunden.

bei Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)

bei LmiRuleScript.Main()

bei Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)

bei iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

 

Regards

Martin

 

 

 

 

0 Likes
Message 9 of 12

Anonymous
Not applicable
Accepted solution

Sorry one more bug.. (Like I said I didn't test..)

If type of component is virtual component the displayname is under the 'Definition' but with normal component(ipt, iam) displayname is under the document object. So we need to read the displayname only from virtual components.

 

Dim oOcc As ComponentOccurrence
Try
       oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual(GoExcel.CellValue("Z2"), oMatrix)
Catch ex As Exception
      For Each oOcc In oAsm.ComponentDefinition.Occurrences

          If TypeOf oOcc.Definition Is VirtualComponentDefinition Then 'compare only virtual component names 
             If oOcc.Definition.Displayname = GoExcel.CellValue("Z2") Then
                  oOcc.Delete()
             End If

          End if
      Next
oOcc = oAsm.ComponentDefinition.Occurrences.AddVirtual(GoExcel.CellValue("Z2") , oMatrix)


End Try

 

Off topic: I don't use(and like) iLogic because no debugging available and this topic is good example where debug would be very helpful. I prefer vb.net and VBA. I also think that it would be better to start programming to Inventor with VBA. It would give much more understanding how Inventor API works. If you haven't used/learnd VBA I strongly recommend to do that.

Message 10 of 12

GosponZ
Collaborator
Collaborator

Hi Curtis,

your code working just fine, but I have one question. In QTY I need put number like 0.1 but it will not work. Can you advice how my virtual part will be in BOM in decimal?

Thanks

0 Likes
Message 11 of 12

martinhoos
Advocate
Advocate

Thanks RAS - works perfect!

0 Likes
Message 12 of 12

martinhoos
Advocate
Advocate

.

0 Likes