cancel
Showing results for 
Search instead for 
Did you mean: 

InvalidCastException after closing and reopening of a Drawing

Pioneer
Pioneer
Hello all, I am developing a program which generates a tableSection with userAttributes to save the Tags of all the objects related to the application as strings, sothat the user can retrieve the objects and edit them inside the program. the program works fine till i close the drawing file and reopen it again; the program then throws exceptions for these lines. originTargetPoint = (NXOpen.Annotations.TargetPoint)NXOpen.Utilities.NXObjectManager.GetObjectFromUInt(uint.Parse(tablesection.GetStringUserAttribute("originTargetPoint", 0))); originDraftingNote = (NXOpen.Annotations.SimpleDraftingAid)NXOpen.Utilities.NXObjectManager.GetObjectFromUInt(uint.Parse(tablesection.GetStringUserAttribute("originDraftingNote", 0))); this exception appears to the first line System.InvalidCastException: Unable to cast object of type 'NXOpen.NXObject' to type 'NXOpen.Annotations.TargetPoint'. and in other times this exception appears System.Runtime.InteropServices.SEHException: External component has thrown an exception. i repeat the program works fine till i close it and reopen it again. Any suggestions will be appreciated thanks in advance
5 REPLIES

Re: InvalidCastException after closing and reopening of a Drawing

Phenom
Phenom

Tags of objects are not guaranteed to have the same values from session to session. In other words, you cannot depend on the tag value to point to the same object once you close and reopen the part.

 

You could give the objects custom names or attributes, then look for the names/attributes on subsequent runs of the code. Alternately, you could use the object ID rather than the tag value; but searching through all the objects for a particular ID may be a bit tricky...

Re: InvalidCastException after closing and reopening of a Drawing

Pioneer
Pioneer
OH it will be a big problem, my program depends totally on this Tag property. is there anyway to lock the Tag of the objects, if not how could i get the ID of the object??

Re: InvalidCastException after closing and reopening of a Drawing

Esteemed Contributor
Esteemed Contributor

- There are "handles" (which supposedly persist across sessions, but I've never used them)

- As already mentioned - use a unique naming/attributing scheme

- Use "user defined objects" (UDOs) - you can store links to other objects (pass in a tag?) but those links are "persistant" (they will last across sessions).  In addition you can specify behavior (e.g. "what happens to the UDO if the other object is deleted?")

 

Note the UDO can store store more than 1 tag, it can store other data (strings/integers/floating point numbers, etc.) It can show up as a feature in the Part Navigator (or not) depending on what you need.  It can also trigger your own "edit" dialog (I believe only if a feature)

 

Ken

Ken Akerboom Sr CAx Systems Engr, Moog, Inc.
Production: NX10.0.3.5 MP5 + patch/TC11.2
I'd rather be e-steemed than e-diseaseled


Re: InvalidCastException after closing and reopening of a Drawing

Genius
Genius

If have used the handles mentioned by Ken to externally store and restore references to objects in a xml file across sessions (UF_TAG_ask_handle_from_tag, UF_TAG_ask_tag_of_handle) without any problems.

Those handles are simple strings so you can store them in an attribute just as well.

 

Regards

Jan

Jan Böttcher

Re: InvalidCastException after closing and reopening of a Drawing

Pioneer
Pioneer
many thanks to all of you for your help. i have solved the problem using UF_TAG_ask_handle_from_tag, UF_TAG_ask_tag_of_handle without any difficulty.