Hi
I would like to be able to add iproperty values to a derived part from the assembly..!
Breaking it down..:
When i run the rule, it creates a derived part from the Assembly i have open.
The iproperties from the assembly should be copied to the new Derived Part. or just set direclty from the rule, doesn't matter...
I hope it´s possible, will save us a lot of time..!
/LSA-Skan
Hi there,
In VBA you could do something like this to copy the iProperties from one file to the other:
Sub CopyIproperties(ByVal fromDoc As Document, ByVal toDoc As Document) Dim fromPs As PropertySet For Each fromPs In fromDoc.PropertySets Dim toPs As PropertySet If Not toDoc.PropertySets.PropertySetExists(fromPs.InternalName) Then Set toPs = toDoc.PropertySets.Add(fromPs.name, fromPs.InternalName) Else Set toPs = toDoc.PropertySets(fromPs.InternalName) End If Dim fromP As Property For Each fromP In fromPs Dim toP As Property On Error Resume Next Set toP = toPs.ItemByPropId(fromP.PropId) If Err Then Set toP = toPs.Add(fromP.Value, fromP.name, fromP.PropId) Else Set toP = toPs(fromP.name) If Not IsNull(fromP.Value) Then toP.Value = fromP.Value End If End If On Error GoTo 0 Next Next End Sub Sub CopyIpropertiesTest() Dim toDoc As Document Set toDoc = ThisApplication.ActiveDocument Dim fromDoc As Document Set fromDoc = ThisApplication.Documents.Open("C:\Part1.ipt", False) CopyIproperties fromDoc, toDoc End Sub
You should be able to convert this code to be used from an iLogic rule.
Let us know if you're having issues with the conversion.
I hope this helps.
Cheers,
Hi Adam
First of all thanks for your reply, and sorry for my late response... just got back after a holiday 🙂
I might need a little more help in getting this working for me..!
First of all, im not experienced with VBA, so im not sure what the different lines does.
If i just Copy Paste it into my rule, it goes.: "Error in rule program Format" And i don't know how to convert it into iLogic code.
At the buttom of your code you do a copyIpropertyTest on a specific Path. Im not sure how this test works, if the rule runs, but in my case i have a specific Path, but the filename changes every time.
The "Copy iproperty" rule should run with this rule, which creates a derived part from the assembly i have open..:
'set a reference to the assembly component definintion.'this assumes an assembly document is open.Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition 'define the path and file nameDim sPathandName As String sPathandName = ThisDoc.PathAndFileName(False) ' Create a new part file to derive the selected part into 'note: kPartDocumentObject is the default template'you could specifiy a path and file name for a custom templateDim oPartDoc As PartDocument oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject)) 'create the derived assembly deffinitionDim oDerivedAssemblyDef As DerivedAssemblyDefinition oDerivedAssemblyDef = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(sPathandName & ".iam") 'Set level of DetailoDerivedAssemblyDef.ActiveLevelOfDetailRepresentation = "Konfigurator" 'Set derive style optionsoDerivedAssemblyDef.DeriveStyle = DerivedComponentStyleEnum.kDeriveAsSingleBodyWithSeams 'options: 'kDeriveAsSingleBodyWithSeams 'kDeriveAsSingleBodyNoSeams 'kDeriveAsMultipleBodies 'kDeriveAsWorkSurface 'Create the derived partDim oDerivedAssembly As DerivedAssemblyComponent oDerivedAssembly = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAssemblyDef)
Hi,
Here is the iLogic version of the code:
Sub Main() Dim toDoc As Document toDoc = ThisApplication.ActiveDocument Dim fromDoc As Document fromDoc = ThisApplication.Documents.Open("C:\Part1.ipt", False) CopyIproperties(fromDoc, toDoc) ' Skip save fromDoc.Close (True) End Sub Sub CopyIproperties(ByVal fromDoc As Document, ByVal toDoc As Document) Dim fromPs As PropertySet For Each fromPs In fromDoc.PropertySets Dim toPs As PropertySet If Not toDoc.PropertySets.PropertySetExists(fromPs.InternalName) Then toPs = toDoc.PropertySets.Add(fromPs.Name, fromPs.InternalName) Else toPs = toDoc.PropertySets(fromPs.InternalName) End If Dim fromP For Each fromP In fromPs Dim toP On Error Resume Next toP = toPs.ItemByPropId(fromP.PropId) If Err.Number <> 0 Then toP = toPs.Add(fromP.Value, fromP.Name, fromP.PropId) Else toP = toPs(fromP.Name) If fromP.Value <> Nothing Then toP.Value = fromP.Value End If End If On Error Goto 0 Next Next End Sub
I hope this helps.
Cheers,
HI Adam
Thanks for the conversion.. 🙂
i have 3 things...:
1. when i run the rule with my configurator rule it fails, posting this error..:
Error on Line 2 : Declaration expected.
Error on Line 6 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 8 : Declaration expected.
Error on Line 14 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 15 : Declaration expected.
Error on Line 16 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 17 : Declaration expected.
Error on Line 18 : 'End If' must be preceded by a matching 'If'.
Error on Line 21 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 22 : Declaration expected.
Error on Line 23 : Declaration expected.
Error on Line 24 : Declaration expected.
Error on Line 25 : Declaration expected.
Error on Line 26 : Declaration expected.
Error on Line 27 : Declaration expected.
Error on Line 28 : Declaration expected.
Error on Line 29 : Declaration expected.
Error on Line 30 : Declaration expected.
Error on Line 31 : Declaration expected.
Error on Line 32 : 'End If' must be preceded by a matching 'If'.
Error on Line 33 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 34 : Declaration expected.
Error on Line 35 : Declaration expected.
Error on Line 36 : Declaration expected.
Error on Line 37 : Declaration expected.
Error on Line 38 : Declaration expected.
Error on Line 39 : Declaration expected.
Error on Line 40 : Declaration expected.
Error on Line 41 : Declaration expected.
Error on Line 42 : Declaration expected.
Error on Line 43 : Declaration expected.
Error on Line 44 : 'End If' must be preceded by a matching 'If'.
Error on Line 45 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 46 : Declaration expected.
Error on Line 47 : Declaration expected.
Error on Line 48 : Declaration expected.
Error on Line 49 : Declaration expected.
Error on Line 50 : Declaration expected.
Error on Line 51 : Declaration expected.
Error on Line 52 : Declaration expected.
Error on Line 53 : Declaration expected.
Error on Line 54 : Declaration expected.
Error on Line 55 : Declaration expected.
Error on Line 56 : 'End If' must be preceded by a matching 'If'.
Error on Line 57 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 58 : Declaration expected.
Error on Line 59 : Declaration expected.
Error on Line 60 : Declaration expected.
Error on Line 61 : Declaration expected.
Error on Line 62 : Declaration expected.
Error on Line 63 : Declaration expected.
Error on Line 64 : Declaration expected.
Error on Line 65 : Declaration expected.
Error on Line 66 : Declaration expected.
Error on Line 67 : Declaration expected.
Error on Line 68 : 'End If' must be preceded by a matching 'If'.
Error on Line 72 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 73 : Declaration expected.
Error on Line 74 : Declaration expected.
Error on Line 75 : Declaration expected.
Error on Line 76 : Declaration expected.
Error on Line 77 : Declaration expected.
Error on Line 78 : Declaration expected.
Error on Line 79 : Declaration expected.
Error on Line 80 : Declaration expected.
Error on Line 81 : Declaration expected.
Error on Line 82 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 83 : Declaration expected.
Error on Line 84 : Declaration expected.
Error on Line 85 : 'End If' must be preceded by a matching 'If'.
Error on Line 86 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 87 : Declaration expected.
Error on Line 88 : Declaration expected.
Error on Line 89 : 'End If' must be preceded by a matching 'If'.
Error on Line 90 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 91 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 92 : Declaration expected.
Error on Line 93 : Declaration expected.
Error on Line 94 : 'End If' must be preceded by a matching 'If'.
Error on Line 95 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 96 : Declaration expected.
Error on Line 97 : Declaration expected.
Error on Line 98 : Declaration expected.
Error on Line 99 : 'End If' must be preceded by a matching 'If'.
Error on Line 100 : 'End If' must be preceded by a matching 'If'.
Error on Line 101 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 102 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 103 : Declaration expected.
Error on Line 104 : Declaration expected.
Error on Line 105 : 'End If' must be preceded by a matching 'If'.
Error on Line 106 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 107 : Declaration expected.
Error on Line 108 : 'End If' must be preceded by a matching 'If'.
Error on Line 109 : 'End If' must be preceded by a matching 'If'.
Error on Line 110 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 111 : Declaration expected.
Error on Line 112 : Declaration expected.
Error on Line 113 : 'End If' must be preceded by a matching 'If'.
Error on Line 114 : 'End If' must be preceded by a matching 'If'.
Error on Line 116 : Syntax error.
Error on Line 119 : Statement cannot appear outside of a method body/multiline lambda.
Error on Line 0 : Maximum number of errors has been exceeded.
all these errors are in the beginning of the rule, and I inserted your code in the end...?!
2. if i place your code in another rule it seems to work. However i have some Custom properties i would like to copy as well... how do i include this..?
3. the line fromDoc = ThisApplication.Documents.Open("C:\Part1.ipt", False) this name might change from time to time. is it possible to refer to an assembly with the same name as a part..? item1.iam -> item1.ipt..?
Hi LSA-skan,
In default, the rule has not any sub function. So the codes pasted directly in the rule are actually within a Main function, but we do not need to declare it explicitly.
However, if there is any sub function, we must declare the Main function explicitly, like Adam’s code. In such case, your main workflow should within the Main function. And rule begins to run there.
In a word,
Your other rule codes >> ‘ Do not put outside Main()! Sub Main() ‘Your other rule codes should be within Main function End Sub Sub OtherFunction() End Sub
Hi again
Sorry for the wait time again, have been put on other projects.. 😞
Now i have tried this code, and its working out for me now, except i get an error messages as follows..:
System.ArgumentException: Forkert parameter. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.PropertySets.Add(String Name, Object InternalName)
at LmiRuleScript.CopyIproperties(Document fromDoc, Document toDoc)
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
Besides the error message it does what it´s supossed, so im not realy sure what is wrong.. 😕
Hi,
Are you using the code in message 4 Adam provided? or you added something else? Please share a code demo for diagnose.
Hi Xiaodong
Here is the code im using. Although i have a slight suspicion that the error is connected to the code actually doing the derived, as is sometimes comes already when running that..! but this copy iprop seems to trigger it every time.!
Sub Main() Dim PartN As String PartN = iProperties.Value("Project", "Part Number") Dim fromDoc As Document fromDoc = ThisApplication.ActiveDocument Dim toDoc As Document toDoc = ThisApplication.Documents.Open("M:\tegn\Standard bibliotek\80000 Skanroll standard\" & PartN & ".ipt", False) CopyIproperties(fromDoc, toDoc) ' Skip save fromDoc.Close (True) End Sub Sub CopyIproperties(ByVal fromDoc As Document, ByVal toDoc As Document) Dim fromPs As PropertySet For Each fromPs In fromDoc.PropertySets Dim toPs As PropertySet If Not toDoc.PropertySets.PropertySetExists(fromPs.InternalName) Then toPs = toDoc.PropertySets.Add(fromPs.Name, fromPs.InternalName) Else toPs = toDoc.PropertySets(fromPs.InternalName) End If Dim fromP For Each fromP In fromPs Dim toP On Error Resume Next toP = toPs.ItemByPropId(fromP.PropId) If Err.Number <> 0 Then toP = toPs.Add(fromP.Value, fromP.Name, fromP.PropId) Else toP = toPs(fromP.Name) If fromP.Value <> Nothing Then toP.Value = fromP.Value End If End If On Error Goto 0 Next Next End Sub
here is the code doing the Deriving... got it from Curtis Waguespack 🙂
If Gem = True Then 'set a reference to the assembly component definintion.'this assumes an assembly document is open.'Dim oAsmCompDef As AssemblyComponentDefinitionoAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition 'define the path and file nameDim sPathandName As String sPathandName = ThisDoc.PathAndFileName(False) ' Create a new part file to derive the selected part into 'note: kPartDocumentObject is the default template'you could specifiy a path and file name for a custom templateDim oPartDoc As PartDocument oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject)) 'create the derived assembly deffinitionDim oDerivedAssemblyDef As DerivedAssemblyDefinition oDerivedAssemblyDef = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(sPathandName & ".iam") 'Set level of DetailoDerivedAssemblyDef.ActiveLevelOfDetailRepresentation = "Konfigurator" 'Set derive style optionsoDerivedAssemblyDef.DeriveStyle = DerivedComponentStyleEnum.kDeriveAsSingleBodyWithSeams 'options: 'kDeriveAsSingleBodyWithSeams 'kDeriveAsSingleBodyNoSeams 'kDeriveAsMultipleBodies 'kDeriveAsWorkSurface 'Create the derived partDim oDerivedAssembly As DerivedAssemblyComponent oDerivedAssembly = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAssemblyDef) ' Break the link'oDerivedAssembly.BreakLinkToFile()'oDerivedAssembly.SupressLinkToFile() Gem = False End If
Hi Xiaodong
what i mentioned ealier, about the error being related to the other code, is now rejected... I made this simple assembly, and tried the code here... same result, still works but i still get the error..!
Also, as you can see i have a Custom property called "Raavare" that i would like to copy, is that possible?
If you have time to look at it i would be realy happy..!
(Inv 2013)
/LSA
Hi Xiaodong
Thanks for looking at this.. 🙂
The "Gem" is only a boolean i use to active the rule in an iLogic form. (Gem means Save in Danish)
However the code that makes the error is not the one in message 10, but the one in message 9. i had hope it was includded in the tests parts, but i now realize i have it as an external rule.. here it is again..:
Sub Main() Dim PartN As String PartN = iProperties.Value("Project", "Part Number") Dim fromDoc As Document fromDoc = ThisApplication.ActiveDocument Dim toDoc As Document toDoc = ThisApplication.Documents.Open("M:\tegn\Medarbejder Mapper\LSA\" & PartN & ".ipt", False) CopyIproperties(fromDoc, toDoc) ' Skip save fromDoc.Close (True) End Sub Sub CopyIproperties(ByVal fromDoc As Document, ByVal toDoc As Document) Dim fromPs As PropertySet For Each fromPs In fromDoc.PropertySets Dim toPs As PropertySet If Not toDoc.PropertySets.PropertySetExists(fromPs.InternalName) Then toPs = toDoc.PropertySets.Add(fromPs.Name, fromPs.InternalName) Else toPs = toDoc.PropertySets(fromPs.InternalName) End If Dim fromP For Each fromP In fromPs Dim toP On Error Resume Next toP = toPs.ItemByPropId(fromP.PropId) If Err.Number <> 0 Then toP = toPs.Add(fromP.Value, fromP.Name, fromP.PropId) Else toP = toPs(fromP.Name) If fromP.Value <> Nothing Then toP.Value = fromP.Value End If End If On Error Goto 0 Next Next End Sub
/LSA
Hi LSA,
I created a simple assembly ("assembly1.iam) and derived it to a part named "derivepart.ipt". since your ilogic code uses part number of the assembly as the derived part name, I changed the part number of the assembly to "derivepart". And I copied your code to assembly. Finally, ran the ilogic code, it works well. All properties are copied from assembly to part. I did not get a an error.
the test dataset is attached. ilogic code is enclosed with the assembly.
Hi Xiaodong
Im affraid i can't open your components, since im only using version 2013. did you have any luck with the parts i posted? im thinking maybe the error is not happening in 2014?
What im actually looking for is to transfer iProperties both ways, so im looking for something like the thread i have here started up here..:
Im thinking it should be a fairly easy piece of code to the trained VBA/iLogic user..!?
/LSA
Hi LSA,
sorry for the inconvenience. The 2013 dataset is attached. to run it:
- place the assembly and part to c:\temp because the ilogic code hard coded it
- open the assembly. run its ilogic code. The code will copy the iproperties of assembly to the part which derives it.
Hope this help you diagnose the problem at your side.
Im affraid this rule does the exact same thing... Works but an error pops up...
as i cannot make anything from this error message, im thinking i need to activate something, or need an Addin in the DDL Directory, or what ever..?!
is it not possible to make a realy simple rule even I can adjust..
Something like this,
copy iproperty value "X" from File "Y" to File "Z"
copy iproperty value "X2" from File "Z" to File "Y"
It doesn't have to be super fast, just work.. 🙂
(Here is the error message i got when running the rule in your files..)
System.ArgumentException: Forkert parameter. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.PropertySets.Add(String Name, Object InternalName)
at LmiRuleScript.CopyIproperties(Document fromDoc, Document toDoc)
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
/LSA