- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
iLogic to create several model states and assign custom iproperty for each model state
Edit - I decided to go a different direction. No need for the help on this one. Thanks all for looking!
Hey everyone, we just upgraded to Inventor 2022 from 2020 and enjoying model states capability. I've been reading over a few threads and snippets that others have written. Some of them capture a small aspect of what I'm trying to do but I can't seem to pull it all together and functioning. I was hoping this thread would get me close to what I need but so far it only creates a model state for the top level assembly and we don't need dialog boxes just yet https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/example-create-and-activate-model-st....
Previously in 2020 Inventor, we were using a simple snippet to combine the part number iproperty with a "-N" or "-G" etc text but with Model States I'm hoping we can make this even better. Here is the code we were using which is very simple as you can see:
iProperties.Value("Custom", "Finished part number") = iProperties.Value("Project", "Part Number") & "-N"
What I'm trying to do is create an ilogic rule to run on our legacy Inventor 2020 files that will create several model states. I want to the ilogic code to create a string of model states, fetch the Master model state iproperty and populate it into each new model state's part number iproperty but add a suffix of the model state name at the end of the part number.
To give some info on what these values mean. -N is natural metal finish, -G is galvanized part, L-N is a left-hand natural metal finish part, L-G is a left-hand galvanized part, R-N is a right-hand natural metal finish part, R-G is a right-hand galvanized part.
Basically I need to:
- Automatically create "-N", "-G", "L-N", "L-R", "R-N", and "R-G" model states on each part or assembly file
- Populate each new model state part number iproperty with the same part number iproperty as the Master model state and then add a suffix to the part number of each model state that contains the model state's name
Hope that all makes sense. If not here are some pictures showing what I'm trying to achieve...
Model States:
Master Model State Part Number iproperty:
-N Model State Part Number iproperty needs to read as:
L-N Model State Part Number iproperty needs to read as:
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
You can get some insight here:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
@Frederick_Law thanks for the link. Looking into it I've tried to modify it for what I'm trying to achieve but running into errors. I'm sure I haven't coded this correctly lol.
Sub Main() Dim oDoc As PartDocument = ThisDoc.Document Dim oCompDef As PartComponentDefinition = oDoc.ComponentDefinition Dim oModelStates As ModelStates Dim oModelState As ModelState Dim oOccu As ComponentOccurrence Dim oOccuDef As ComponentDefinition Dim oOccuDoc As Document oModelState.Name "-N" iProperties.Value("Project", "Part Number") = oModelState.Name "Master" iProperties.Value("Project", "Part Number") oModelState.Name "-G" iProperties.Value("Project", "Part Number") = oModelState.Name "Master" iProperties.Value("Project", "Part Number") oModelState.Name "L-N" iProperties.Value("Project", "Part Number") = oModelState.Name "Master" iProperties.Value("Project", "Part Number") oModelState.Name "L-G" iProperties.Value("Project", "Part Number") = oModelState.Name "Master" iProperties.Value("Project", "Part Number") oModelState.Name "R-N" iProperties.Value("Project", "Part Number") = oModelState.Name "Master" iProperties.Value("Project", "Part Number") oModelState.Name "R-G" iProperties.Value("Project", "Part Number") = oModelState.Name "Master" iProperties.Value("Project", "Part Number") iProperties.Value("Custom", "Finished part number") = iProperties.Value("Project", "Part Number") & oModelState.Name "-N" iProperties.Value iProperties.Value("Custom", "Finished part number") = iProperties.Value("Project", "Part Number") & oModelState.Name "-G" iProperties.Value iProperties.Value("Custom", "Finished part number") = iProperties.Value("Project", "Part Number") & oModelState.Name "L-N" iProperties.Value iProperties.Value("Custom", "Finished part number") = iProperties.Value("Project", "Part Number") & oModelState.Name "L-G" iProperties.Value iProperties.Value("Custom", "Finished part number") = iProperties.Value("Project", "Part Number") & oModelState.Name "R-N" iProperties.Value iProperties.Value("Custom", "Finished part number") = iProperties.Value("Project", "Part Number") & oModelState.Name "R-G" iProperties.Value End Sub
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Here is something you can try out. I modified this from something I already had, in an attempt to suit your needs. There are several ways to do something like this.
Sub Main
Dim oDoc As Document = ThisDoc.FactoryDocument
If oDoc Is Nothing OrElse ((Not TypeOf oDoc Is PartDocument) And (Not TypeOf oDoc Is AssemblyDocument)) Then
MessageBox.Show("This code only works for a Part or Assembly.", "Wrong Document Type")
Exit Sub
End If
If oDoc.IsModifiable = False Then Exit Sub
'make sure the custom iProperty exists (then it will exist for all ModelStates)
'first get a reference to the custom property set
Dim oPNProp As Inventor.Property = oDoc.PropertySets.Item(3).Item("Part Number")
Dim sPN As String 'this will hold the regular Part Number value later
Dim oCProps As Inventor.PropertySet = oDoc.PropertySets.Item(4)
'now create a variable to hold the custom property
Dim oCProp As Inventor.Property = Nothing
Try 'try to find existing one
oCProp = oCProps.Item("Finished part number")
Catch 'that failed, so create it
oCProp = oCProps.Add("", "Finished part number")
End Try
If oCProp Is Nothing Then Exit Sub 'couldn't find or create the custom iProperty so exit rule
Dim oMSs As ModelStates = oDoc.ComponentDefinition.ModelStates
If oMSs.MemberEditScope <> MemberEditScopeEnum.kEditActiveMember Then
oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
End If
'make sure 'master/primary' ModelState is active, to get Part Number value from
If oMSs.ActiveModelState IsNot oMSs.Item(1) Then
oMSs.Item(1).Activate
End If
oDoc = oMSs.ActiveModelState.FactoryDocument
sPN = oPNProp.Value
Dim oNames() As String = {"-N", "-G", "L-N", "L-R", "R-N", "R-G" }
For Each sName In oNames
Dim oThisMS As ModelState = Nothing
Try
oThisMS = oMSs.Item(sName)
Catch
oThisMS = oMSs.Add(sName)
End Try
oThisMS.Activate
oCProp.Value = sName
oPNProp.Value = sPN & sName
Next
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS)
.
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Well after testing model states on an assembly I did not realize the Bill of Materials is primarily driven by part number iProperty to determine how to treat model states and identify or display them as separate parts in a Parts List. Basically all of the model states can't share the same part number or else my assembly parts list shows a single part number field but with multiple QPA or instances. So now I need to rethink this and basically have the model states populate their part number field with the same master part number iProperty plus the corresponding model state. In essence forgo the population of the custom iProperty and instead use that to the part number iProperty field.
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Actually, I see now that I should have changed a couple of the last lines in the code I posted, because they are not doing what you instructed. The way I posted it, it is setting the custom iProperty to that suffix only, and not the part number plus the suffix, then it is setting the suffix to the end of the regular part number. You wanted the regular part number to remain the same for each ModelState, and only the custom iProperty in each one to include the regular part number plus the suffix. To fix that, just change these two lines near the end:
oCProp.Value = sName
oPNProp.Value = sPN & sName
...like this:
oCProp.Value = sPN & sName
oPNProp.Value = sPN
My mistake.
But yes, your assembly's BOM can only show the contents of one ModelState at a time. Each ModelState is like its own separate Document. So a single file on disk can now have multiple Document definitions within it, with the introduction of ModelStates.
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Each ModelState can have it's own Part Number and other iProperties.
Edit them in MemberScrope and they will be different.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
@WCrihfield Thanks for the snippet. Unfortunately I realized after originally posting that I just need the suffix (model state name) to be added to the corresponding model states' part number iproperty field. I edited your last two lines and it doesn't seem to generate the model states. When I ranit on a template that I already have created the model states, it does populate the finished part number custom iproperty with the model state name but doesn't include the part number. Now that I'm not needing it to write to the custom iproperty and rather write it to the part number iprop of the active model state, where should I alter the code at?
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
@Frederick_Law yes we are using memberscope and I understand tht each model state can have it's own iprops. This is why I'm trying to get the model state name combined with the master part number field and then populate that new combo to the part number field of the corresponding model state.
Example:
Model state Master = part number iprop 12345 ("base" part number)
Model state -N = part number iprop 12345-N (the "base" part number is copied from master model state + model state name)
Model state -G = part number iprop 12345-G (the "base" part number is copied from master model state + model state name)
and so on. Hope that helps explain.
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Edited the first post with what I'm actually needing to do now that I've tested this through with BOM's and Parts List. Hopefully someone can help me achieve this scripting. Many thanks in advance!
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!