Best practice using rules or VB code

Best practice using rules or VB code

JohnKHouston
Advocate Advocate
653 Views
8 Replies
Message 1 of 9

Best practice using rules or VB code

JohnKHouston
Advocate
Advocate

Hello all,

 

I've been using an ilogic rule to find the mass of a part/assembly and display the value in our title block. By using the rule we're able to remove the lbmass suffix and to define the number of decimal places. Unfortunately it sometimes doesn't work so I've been trying to find a substitute solution.

 

I've found a couple different ways that I can record the mass and display the properties properly. One is to use a rule that would set up a custom parameter within the model that could be read as a text string in the drawing title block. For example: https://forums.autodesk.com/t5/inventor-general-discussion/how-to-remove-iproperty-mass-unit-string-...  if you scroll down thru the thread and find LT.Rusty's post.

 

The other method would be using VB code to essentially do the same thing. Example here: http://www.cadalyst.com/cad/autocad/inventor-in-depth-tips-customizing-template-parts-10639  the code for the mass values is near the bottom of the page.

 

My question is, which method would be easiest to implement and update old models/drawings? Can anybody see one solution being better than the other?

 

I'd appreciate some feedback before I get too far into changing how things are done.

 

Thanks in advance!

John

0 Likes
Accepted solutions (1)
654 Views
8 Replies
Replies (8)
Message 2 of 9

Jef_E
Collaborator
Collaborator

For implementing and updating old drawings the best solution ( to me ) would be using the iLogic rule version, and overwrite your old rule with this code.

 

Because you use iLogic now it's easier to stick with it. You can change the existing files their rules by using @MegaJerk his code injector and overwrite the content.

https://github.com/MegaJerk/iLogicCodeInjector

 

 



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
Message 3 of 9

JohnKHouston
Advocate
Advocate

Thanks for the advice, Jef

 

My service provider suggested the same thing (using the ilogic rule) so that's good to see two similar thoughts.

 

The code that we're currently using is actually in the idw file, so I would need to somehow delete it and to add the ilogic code to our parts and assemblies. I should mention also that we're using 2016 Vault pro, so I'm not sure if that makes things any easier or harder. Perhaps someone could comment on that.

 

Again, thanks for your reply. It's very much appreciated!

 

John

0 Likes
Message 4 of 9

Jef_E
Collaborator
Collaborator

If, all files are in vault, there is a downside to all this. They need to be checked out. I don't know any stats: like how many file are there? But no worries the task scheduler can do this for you.

 

For the deleting of the iLogic rule:

 

Open the code injector, browse the files you want to edit. Enter the existing rule name** into the "New rule name" window, check the option overwrite duplicate rule. Run the empty rule and set delete rule after run. I believe that should do the trick. All at once!

 

** I assume this rule name is identical in ALL of the drawings?

 

Adding the new rule to the parts and assemblies.

 

 

The workflow is almost the same. Browse the files, add the rule you want to add, add event triggers, run the rule ( but keep it ). And finished.

 

May I suggest that you write youre code a bit more fail safe than in the example you suggested.

InventorVb.DocumentUpdate()
Parameter("partmass")=iProperties.Mass
InventorVb.DocumentUpdate()

You could add a check to see if the parameter exists. If it doesn't it should be created otherwise the value cannot be stored. I assume that you also would like to add the parameter automatically?

 

If I may ask, after you have a parameter "partmass" how do you get this information into the titleblock?

 



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
Message 5 of 9

JohnKHouston
Advocate
Advocate
Hi Jef,

Thanks for the reply, really appreciate all your advice!

Currently there is a just a label text that gets the value from a custom property in the drawing <TotalWT>. So I would just need to modify that to get the custom property from the model.

I suppose I could call my model parameter the same name <TotalWT> and just modify the titleblock to get the parameter information from the model instead of the drawing.

I'll have to check with my service provider and see if he can code the fail safe check you suggested. Makes sense to me but unfortunately I have no idea how to do any coding for the ilogic rules. I've just been able to find what I need from all the contributors here and use that. 🙂

I suppose I should teach myself but it's always hard to find the time to do those types of things. 😉

Thanks again, you've been really helpful!
John
0 Likes
Message 6 of 9

Jef_E
Collaborator
Collaborator

I'll have to check with my service provider and see if he can code the fail safe check you suggested. Makes sense to me but unfortunately I have no idea how to do any coding for the ilogic rules. I've just been able to find what I need from all the contributors here and use that. 🙂

I suppose I should teach myself but it's always hard to find the time to do those types of things. 😉

You should not be paying for such a simple code. That is absurd 😉



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 7 of 9

JohnKHouston
Advocate
Advocate
Yeah, you're right but I wouldn't be charged for that, I hope. 🙂

I just don't know how to do the code so I'll ask them if they'll help, that's all I meant.

Cheers!
0 Likes
Message 8 of 9

Jef_E
Collaborator
Collaborator

Let me see if I can put something together 🙂



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 9 of 9

Jef_E
Collaborator
Collaborator
Accepted solution

You could still ask them to make something, and check their code with mine? I made this piece of code. Only thing i'm not sure off, is the units you are using?

 

Now it's in Kilogram ( I think the default for Inventor )

 

The mass is set into the custom iProperties, this is easy available for titleblocks. I think a parameter is not needed..

If you specify how the mass should be formatted I could adjust that too.

The code is interchangeable for assemblies and parts.

 

' Get the current application
Dim oInvApp As Application
oInvApp = ThisApplication

' Get the active document
Dim oDoc As Document
oDoc = oInvApp.ActiveDocument

' Get the userparameters object
Dim oPropSet As PropertySet
oPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties")

' Set an empty parameter, base value is nothing.
' We will check it later
Dim oProp As [Property]
oProp = Nothing

' Try to get the parameter if it exists,
' if it doesn't create it.
Try
    oProp = oPropSet.Item("partmass")
Catch ex As Exception
    oProp = oPropSet.Add(0, "partmass")
End Try

' Check the parameter object
' if it's still nothing return to end the sub.
If oProp Is Nothing Then
    Return
End If

' Update the document
oDoc.Update()

' Get the mass
Dim oMass As Double

' Check if it'a an partdocument
' This code can only be inserted into parts and assemblies If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then oMass = CType(oDoc, PartDocument).ComponentDefinition.MassProperties.Mass Else oMass = CType(oDoc, AssemblyDocument).ComponentDefinition.MassProperties.Mass End If ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' !!! Do something with the mass here !!! ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! oMass = Math.Round(oMass, 2) ' Set the parameter value to oProp.Value = oMass ' Update the document again oDoc.Update()


Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2