external ilogic rule to creat custom iproperty

external ilogic rule to creat custom iproperty

Anonymous
Not applicable
4,304 Views
16 Replies
Message 1 of 17

external ilogic rule to creat custom iproperty

Anonymous
Not applicable

Guys,

Cannot get this code to run as an external rule. It fails when it tries to create the iproperty "PropertyName1"

I think its to do with the "ThisApplication.ActiveDocument" part but not sure

any ideas?

 

' checks to see if custom iproperty etch_number exists, creates it if it is not in document
' sets value of EtchNumber iprop

Dim TempEtchNumber As String = iProperties.Value("Project", "Part Number")
TempEtchNumber= TempEtchNumber.Replace("-","")
TempEtchNumber =TempEtchNumber.substring(4)
Dim PropertyName1 As String = "EtchNumber"

oCustomPropertySet = ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties")

'look for custom iproperty and try to use it

Try

oProp = oCustomPropertySet.Item(PropertyName1)

Catch
' iproperty not found , create and assign value
oCustomPropertySet.Add("",PropertyName1)
End try
'iProperties.Value("Custom","EtchNumber") = TempEtchNumber

 

 

 I am using INV 2009

 

0 Likes
Accepted solutions (1)
4,305 Views
16 Replies
Replies (16)
Message 2 of 17

Owner2229
Advisor
Advisor

Hi,

the problem is in this "Inventor User Defined Properties",

it should be just "User Defined Properties".

 

Here is your updated code:

 

' Checks to see if custom iproperty etch_number exists, creates it if it is not in document
' Sets value of EtchNumber iprop

Dim oEtchNumber As String = iProperties.Value("Project", "Part Number")
oEtchNumber = oEtchNumber.Replace("-","")
oEtchNumber = oEtchNumber.substring(4)
Dim oProName As String = "EtchNumber" Dim oProSet As Inventor.PropertySet = ThisApplication.ActiveDocument.PropertySets.Item("User Defined Properties") ' Look for custom iproperty and try to use it Dim oProp As Inventor.Property Try oProp = oProSet.Item(oProName) Catch ' iProperty not found , create and assign value oProp = oProSet.Add("",oProName) End try
oProp.Expression = oEtchNumber
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 17

Anonymous
Not applicable

Thanks for posting that code.

However for me it crashes inventor at this line

 oProp = oProSet.Add("",oProName)

My original code fails at the same line

So problem is creating the iproperty. I do think it may be caused by running this as an external rule 

0 Likes
Message 4 of 17

MechMachineMan
Advisor
Advisor

I tried running your code as a rule and it seems to work fine.

 

Are you sure the file(s) you are trying to do this to can be written to? (Are they maybe library parts or read only?)

 

A good check would be trying to manually add the property to the exact same file you are trying to do this to and see if that works.

 

It's also possible the inventor version you are using has different functionality for the Property Sets than what you are using.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 5 of 17

Anonymous
Not applicable

yes the files can be written to

I pasted the code (yours and mine) into a rule within the part

Both created the iprop and populated it.

So it seems that running it as an external rule is the issue

0 Likes
Message 6 of 17

mrattray
Advisor
Advisor

 

Your Try Catch is redundant if you're only trying to set the value. Inventor will automatically create the property if it doesn't exist, but more importantly your syntax is all wrong for setting the value.

 

Try replacing this:

Try

oProp = oCustomPropertySet.Item(PropertyName1)

Catch
' iproperty not found , create and assign value
oCustomPropertySet.Add("",PropertyName1)
End try
'iProperties.Value("Custom","EtchNumber") = TempEtchNumber

With this:

oCustomPropertySet.Item("EtchNumber").Value = TempEtchNumber
Mike (not Matt) Rattray

0 Likes
Message 7 of 17

Anonymous
Not applicable

Matt,thanks for the reply

Your statement that the original syntax is all wrong, does not address the fact that the code runs when placed in a ilogic rule in the part but not when in a external rule.

I tried removing the try catch and replacing it with

oCustomPropertySet. Item ("EtchNumber").Value = TempEtchNumber

as you suggested, that gives me a "unspecified error"

 

 

 

 

 

0 Likes
Message 8 of 17

MechMachineMan
Advisor
Advisor

I changed the first iProp call to the actual API format that doesn't include the iLogic add-in, and added some error checking so hopefully this will work better for you.

 

Also, I made my own .txt file so I could test this as an external rule in the active doc, and everything worked fine. I know iLogic snippets have issues working in external rules if those rules are called to run by a different rule.

 

Also, if this still errors out, could you post a picture of both error windows so we can take a better look at what's happening?

 

Dim oEtchNumber As String

Try
	oEtchNumber = ThisApplication.ActiveDocument.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
	oEtchNumber = oEtchNumber.Replace("-","")
	oEtchNumber = oEtchNumber.substring(4)
Catch
	MsgBox("Error Fetching Part Number!")
End Try

Dim oProName As String = "EtchNumber"
Dim oProSet As Inventor.PropertySet = ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties")

Dim oProp As Inventor.Property
Try
    oProp = oProSet.Item(oProName)
Catch
    ' iProperty not found , create and assign value
    oProp = oProSet.Add("",oProName)
End Try

oProp.Expression = oEtchNumber

 

Thanks,

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 9 of 17

Curtis_Waguespack
Consultant
Consultant
Accepted solution

HI swordmaster,

 

I don't know what it is, but there is an "bad" charachter or something in your oCustomPropertySet line. I couldn' figure it out, but when I pasted in from a rule I had on hand it worked.

 

 

 

This worked for me (Inventor 2015)

 

Dim PropertyName1 As String = "EtchNumber"
oCustomPropertySet = ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties")

'look for custom iproperty and try to use it
Try
oProp = oCustomPropertySet.Item(PropertyName1)
Catch
' iproperty not found , create and assign value
oCustomPropertySet.Add("",PropertyName1)
End Try
iProperties.Value("Custom","EtchNumber") = "TempEtchNumber"

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

 

EESignature

Message 10 of 17

Anonymous
Not applicable

Guys,

I appreciate all of your responses.

 

Curtis, The "bad" character is a good thought, however it does not really explain why the same code works when i cut and paste it into the part and run it from there as opposed to running it as a external rule.

 

I do have access to INV 2016 i will try running the code on that system. Perhaps there is a problem with INV 2009 (which i am using at work)

0 Likes
Message 11 of 17

Curtis_Waguespack
Consultant
Consultant

This demonstates the issues as I was seeing it:

 

Dim oDoc as Document
oDoc = ThisApplication.ActiveDocument

Dim oCustomPropertySet As PropertySet
'oCustomPropertySet = oDoc.PropertySets.Item("​Inventor User Defined Properties")
oCustomPropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
MessageBox.Show("Message1", "iLogic")

oCustomPropertySet = oDoc.PropertySets.Item("​Inventor User Defined Properties")
'oCustomPropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
MessageBox.Show("Message2", "iLogic")

 

 

looks like the same code, I comment out the 1st line and run the 2nd line, and it makes it to the message, then I comment out the 2nd line and run the first line and it errors out.

 

I can't explain it, as they appear to be the same lines. Smiley Frustrated

 

p.s. all my testing was as an external rule

EESignature

0 Likes
Message 12 of 17

Curtis_Waguespack
Consultant
Consultant

This was driving me crazy so I went character by character and found some "weirdness" between the " and the I in the line as shown: 

 

oCustomPropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")

 

When I take ot the bad character your code runs in an internal and external rule without issue, but maybe I'm only solving an issue introduced by copying and pasting from this forum, and not the actual issue you're seeing in Inventor 2009. Smiley Frustrated

EESignature

0 Likes
Message 13 of 17

Anonymous
Not applicable

Thanks Curtis,

I have seen many instances of "weird characters" being introduced when copying code from the forum.

As you say this may have been introduced during the posting of this problem.

However i will delete that particular line from my original code  and re-type it, hopefully that will solve the mystery.

0 Likes
Message 14 of 17

Curtis_Waguespack
Consultant
Consultant

@mrattray wrote:

... Inventor will automatically create the property if it doesn't exist...

 


Hi mrattray,

 

I've never been able to get Inventor to automatically create an iProperty if it doesn't exist without using the Try/Catch, I've likely just missed something though.

 

Can you provide a working example? I tried using this, but didn't get it to work.

 

 

oCustomPropertySet = ThisApplication.ActiveDocument.PropertySets.Item("User Defined Properties")
oCustomPropertySet.Item("EtchNumber").Value = "TempEtchNumber"

 

Thanks,
Curtis

EESignature

0 Likes
Message 15 of 17

mrattray
Advisor
Advisor

I'm sorry, guys, I need to stop replying to code questions from memory. This is embarrassing, but the code I posted earlier doesn't work for me, either.

 

@Curtis_Waguespack

I use this line in external (and internal) rules all the time. If "My Property" exists it sets the value to "My Value", otherwise it creates the property and then assigns the value. Really, even the code I posted earlier was unnecessarily complex, since it's just operating on active document anyway. (I actually tested it this time)

 

iProperties.Value("Custom", "My Property") = "My Value"

@Anonymous

Is there a reason we need to explicitly get the property set in this application?

 

Edit: Also, your code as posted in the OP works fine for me in an external rule.

Mike (not Matt) Rattray

Message 16 of 17

Anonymous
Not applicable

Guys,

Finally got this to run. I think Curtis was on the right track with the "weird characters" idea.

I typed out the code again from scratch. I have pasted it below.

 

@mrattray

"Is there a reason we need to explicitly get the property set in this application?" 

To be honest i found a code snippet and used that

Also Inventor (at least 2009 version) would not just create the iproperty if it did not exist. It just told me it could not find the iproperty.

 

Here is my code, based on everyones input.........thanks

 

' start ilogic code


' checks to see if iproperty EtchNumber exists. If not create it and assign a value


Sub Main
'check to see if active document is a part
If ThisApplication.ActiveDocumentType <> 12290 then
MessageBox.Show ("You need to have a part(ipt) active" & vbCrlf & "to run this rule", "ilogic", _
MessageBoxButtons.Ok,MessageBoxIcon.Exclamation)
Exit Sub
End if

Dim TempEtchNumber As String = iProperties.Value("Project","Part Number")

' check part number

If Len(TempEtchNumber) <> 15 then
MessageBox.Show("Add part number to the part number iproperty " & vbCrfl & "run rule again", "ilogic", _
MessageBoxButtons.Ok,MessageBoxIcon.Exclamation)

Exit Sub
End if

TempEtchNumber = TempEtchNumber.Replace("-","")
TempEtchNumber = TempEtchNumber.substring(4)


Dim PropertyName1 As String = "EtchNumber"
oCustomPropertySet = ThisApplication.ActiveDocument.PropertySets.Item ("Inventor User Defined Properties")

'look for custom iproperty and try to use it
Try
oProp = oCustomPropertySet.Item (PropertyName1)
Catch
oCustomPropertySet.Add(" ",PropertyName1)
End Try
iProperties.Value("Custom", "EtchNumber") = TempEtchNumber

End Sub

 

' finish ilogic code

Message 17 of 17

cadman777
Advisor
Advisor

Curtis,

 

This worked perfectly for me.

Thanx!

 

I'm trying to use it in a bigger macro to add missing iProperties to assembly files to use the result in a Parts List title. I'm trying to run it from a drawing file but can't figure out the coding. It's really hard to figure it out when I don't have references to all the data at my finger tips. It's like juggling mass hysteria on the tip of a pin while tight roping across the great divide on a piano wire!

 

Anyways, I may request help if I can't figure it out soon.

 

Thanx again ...

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
0 Likes