Inventor Customization

Reply
Valued Contributor
LSA-skan
Posts: 95
Registered: ‎08-09-2012
Message 1 of 17 (951 Views)

iLogic: Set iproperties in a derived part

951 Views, 16 Replies
01-28-2013 11:39 PM

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

 

ADN Support Specialist
adam.nagy
Posts: 521
Registered: ‎03-26-2007
Message 2 of 17 (922 Views)

Re: iLogic: Set iproperties in a derived part

02-08-2013 06:36 AM in reply to: 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,



Adam Nagy
Developer Technical Services
Autodesk Developer Network
Valued Contributor
LSA-skan
Posts: 95
Registered: ‎08-09-2012
Message 3 of 17 (906 Views)

Re: iLogic: Set iproperties in a derived part

02-13-2013 11:13 PM in reply to: adam.nagy

Hi Adam

 

First of all thanks for your reply, and sorry for my late response... just got back after a holiday :smileyhappy:

 

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)
ADN Support Specialist
adam.nagy
Posts: 521
Registered: ‎03-26-2007
Message 4 of 17 (885 Views)

Re: iLogic: Set iproperties in a derived part

02-20-2013 08:35 AM in reply to: LSA-skan

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,



Adam Nagy
Developer Technical Services
Autodesk Developer Network
Valued Contributor
LSA-skan
Posts: 95
Registered: ‎08-09-2012
Message 5 of 17 (878 Views)

Re: iLogic: Set iproperties in a derived part

02-20-2013 11:26 PM in reply to: adam.nagy

HI Adam

 

Thanks for the conversion.. :smileyhappy:

 

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..?

ADN Support Specialist
xiaodong.liang
Posts: 1,233
Registered: ‎06-12-2011
Message 6 of 17 (846 Views)

Re: iLogic: Set iproperties in a derived part

02-27-2013 06:51 PM in reply to: LSA-skan

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

 

 



Xiaodong Liang
Developer Technical Services
Autodesk Developer Network

Valued Contributor
LSA-skan
Posts: 95
Registered: ‎08-09-2012
Message 7 of 17 (731 Views)

Re: iLogic: Set iproperties in a derived part

05-27-2013 03:05 AM in reply to: xiaodong.liang

Hi again

 

Sorry for the wait time again, have been put on other projects.. :smileysad:

 

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.. :-/

ADN Support Specialist
xiaodong.liang
Posts: 1,233
Registered: ‎06-12-2011
Message 8 of 17 (716 Views)

Re: iLogic: Set iproperties in a derived part

05-31-2013 12:18 AM in reply to: LSA-skan

Hi,

 

Are you using the code in message 4 Adam provided? or you added something else? Please share a code demo for diagnose.



Xiaodong Liang
Developer Technical Services
Autodesk Developer Network

Valued Contributor
LSA-skan
Posts: 95
Registered: ‎08-09-2012
Message 9 of 17 (713 Views)

Re: iLogic: Set iproperties in a derived part

05-31-2013 12:34 AM in reply to: xiaodong.liang

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
Valued Contributor
LSA-skan
Posts: 95
Registered: ‎08-09-2012
Message 10 of 17 (712 Views)

Re: iLogic: Set iproperties in a derived part

05-31-2013 12:36 AM in reply to: xiaodong.liang

here is the code doing the Deriving... got it from Curtis Waguespack :smileyhappy: 

 

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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community


Inventor Exchange Apps

Created by the community for the community, Autodesk Exchange Apps for Autodesk Inventor helps you achieve greater speed, accuracy, and automation from concept to manufacturing.

Connect with Inventor

Twitter

Facebook

Blogs

Pinterest

Youtube