Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get model state to change display name

10 REPLIES 10
Reply
Message 1 of 11
J.VandeMerckt
225 Views, 10 Replies

Get model state to change display name

Hi Forum

 

I have this code which allows me to change the display name with a Property defined in the code.
To make it work with Modelstates I have a if statement which looks if the current display name contains the new display name (modelstate will override the current display name by nature and add the name of the modelstate behind the old display name)

But this Contain function isn't fully working for me.
I'd rather adapt the code to check if a modelstate is active and not the primary.
If another modelstate than primary is active I need to get that name in a string and I can use it in the rest of my code.

Does anybode know how to get the modelstate name in a string (when not primary)?
It should work on parts and assemblies

Here is the current code btw:

'Added If statement for Blank value of PTC_WM_Name
'Added Log
'Adapted code to work with modelstates -> Contain doesn't work

Try
	Dim currentDisplayName As String = ThisDoc.Document.DisplayName
	Dim currentTime As String = Format(Now, "dd-MM-yyy hh:mm")
	Dim newDisplayName As String = iProperties.Value("Custom", "PTC_WM_NAME")

	If iProperties.Value("Custom", "PTC_WM_NAME") = "" Then
		Logger.Info(currentTime + " Windchill Name is Blank")
	ElseIf Not currentDisplayName.Contains(newDisplayName) Then
		ThisDoc.Document.DisplayName = iProperties.Value("Custom", "PTC_WM_NAME")
		Logger.Info(currentTime + " - Changed Display Name from:      " + currentDisplayName + "      to      " + newDisplayName, "Display Name Info")	
	Else
		Logger.Info(currentTime + " - Display Name Unchanged:	" + currentDisplayName)
	End If
Catch
End Try




10 REPLIES 10
Message 2 of 11

Dim oModelState As ModelState
Dim oMSName As String

oMSName = oModelState.Name
Logger.Info(oMSName)
'Check if ModelState name is "Primary"
If oMSName <> ThisServer.LanguageTools.CurrentPrimaryModelStateString Then
'Not Primary
else
'Primary
End If
Message 3 of 11
WCrihfield
in reply to: J.VandeMerckt

Hi @J.VandeMerckt.  You may be able to use the ReadOnly Document.ModelStateName property (String) to get the name of the ModelState that is currently effecting the Document object you are working with.  There is also the 'ThisDoc.ActiveModelState' property, which is Read/Write String.  However, using a custom iProperty may not work the way you want it to, because it can potentially have a different value for each ModelState (or same value).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 11
J.VandeMerckt
in reply to: WCrihfield

The custom Iproperty I'm using should be the same for all modelstates.
I just need to push this Custom Iproperty to the display name and if a model state is active the display name should be:
Custom Iproperty + "(" + Active Modelstate + ")"
This way I'm "mimicking" the behaviour of a modelstate where it uses the display name and places the name of the modelstate behind the display name. except when it's the primary Modelstate. Then it just uses the display name.

Actually could be an Inventor Idea to be able to change the property the display name is using.
That would solve all my problems.

Message 5 of 11

I've tested this rule on a assembly with a Modelstate activated but i get this error.

Error on line 4 in rule: Test, in document: Assembly1 (Test)

Object reference not set to an instance of an object.


I'm quite puzzled about these model states in Ilogic.

Br

Justin

Message 6 of 11
_dscholtes_
in reply to: J.VandeMerckt


@J.VandeMerckt wrote:

I've tested this rule on a assembly with a Modelstate activated but i get this error.

Error on line 4 in rule: Test, in document: Assembly1 (Test)

Object reference not set to an instance of an object.


I'm quite puzzled about these model states in Ilogic.

Br

Justin


It's because @Frederick_Law forgot to assign oModelState (oModelstate = ...) . Then the oModelState.Name property cannot be retrieved in line 4.

Message 7 of 11
WCrihfield
in reply to: J.VandeMerckt

Hi @J.VandeMerckt.  I think that even once you get this code to work the way you want it to, you will want to put this rule under the 'Model State Activated' event in the Event Triggers dialog.  Either that or use a custom event handler code routine to run it. 

Here is another version of some code you could try out for this task.  It includes the tip that @Frederick_Law mentioned.  It gets a language independent version of the Master/Primary ModelState name, to make the code solution more dynamically useful.

 

Dim oDoc As Inventor.Document = ThisDoc.Document
Dim sMSName As String = ThisDoc.ActiveModelState
Dim sPrimaryMSName As String = ThisApplication.LanguageTools.CurrentPrimaryModelStateString
Dim sBaseDisplayName As String
Try : sBaseDisplayName = oDoc.PropertySets.Item(4).Item("BaseDisplayName").Value : Catch : End Try
If sBaseDisplayName = "" Then Return 'since it is 'custom', just in case
Dim sNewDisplayName As String = ""
If sMSName = "" Or sMSName = sPrimaryMSName Then
	sNewDisplayName = sBaseDisplayName
Else
	sNewDisplayName = sBaseDisplayName & "(" & sMSName & ")"
End If
If oDoc.DisplayName <> sNewDisplayName Then
	oDoc.DisplayName = sNewDisplayName
End If

 

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

EESignature

(Not an Autodesk Employee)

Message 8 of 11
WCrihfield
in reply to: J.VandeMerckt

Another case of not testing it before posting.  I just tested some code like what I just posted earlier this morning on a part, and see the issue that seems to be unavoidable.  It sort of seems like there are two different versions of DisplayName, because the 'top node' of the model browser tree shows the value we want, but the title bar of the Inventor application shows a version that includes an extra '(ModelState name)' in it, and when we show the value of ThisDoc.Document.DisplayName, it also includes the extra '(ModelState name)' in it, for some reason.  That must be getting manipulated behind the scenes by Inventor, and may simply be beyond our control to change.

By the way...why are you wanting to do this in the first place.  Just curious.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 11
Frederick_Law
in reply to: _dscholtes_


@_dscholtes_ wrote:


It's because @Frederick_Law forgot to assign oModelState (oModelstate = ...) . Then the oModelState.Name property cannot be retrieved in line 4.


Not forgot.  We're all posting partial example codes.

Message 10 of 11


@J.VandeMerckt wrote:


I'd rather adapt the code to check if a modelstate is active and not the primary.
If another modelstate than primary is active I need to get that name in a string and I can use it in the rest of my code.


If oModelStates.ActiveModelState.Name = ThisServer.LanguageTools.CurrentPrimaryModelStateString Then
'Primary
else
'Not Primary
End If
Message 11 of 11
J.VandeMerckt
in reply to: WCrihfield

Hi Wesley

We're using a 3rd party plm system which generates dumb numbers for our file names.
These dumb numbers are also used in the display name and make it hard for us too see what we're actually working on.

So we wrote a code to change the display name to a custom Iproperty of our choosing.
This we we can see the property in de model tree and on the top of our screen but still see the dumb number in the bottom of the file tab.

JVandeMerckt_0-1717744167175.png

This code runs with the event trigger when opening a document or saving a document.
We can also run it manually when necessary.

It's working fine but now I noticed it isn't ideal when working with modelstates.
Because active modelstates appear to use the display name and paste the name of the modelstate behind the display name.
But then the code want's to work again because in the eyes of the code the display name isn't correct anymore.

So i need to adapt the code to be able to see if a modelstate is active and if the correct display name is part of the current display name (display name + model state)

I've tried this:

	Dim currentDisplayName As String = ThisDoc.Document.DisplayName
	Dim newDisplayName As String = iProperties.Value("Custom", "PTC_WM_NAME")
	
	If iProperties.Value("Custom", "PTC_WM_NAME") = "" Then
		Logger.Info(currentTime + " Windchill Name is Blank")
	ElseIf Not currentDisplayName.Contains(newDisplayName) Then
		ThisDoc.Document.DisplayName = iProperties.Value("Custom", "PTC_WM_NAME")

 So now the code checks if the old display name is part of the new one just like a modelstate would work.

Problem is that this opens different issues.
If the old display name = Test_Copy
And the new one = Test
Then the code also doesn't want to run. Because "Test" is already part of the old display name.
So I need another way of detecting if it's a model state.

Does that make sense? Feel free to ask more questions, if tried to explain the best I can but my knowledge of the English language is limited just as my knowledge of the frikking way modelstates work.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report