Nik Posted November 3, 2019 Share Posted November 3, 2019 (edited) Trying to figure out a quick and easy way to see if a symbol exists in a drawing. There doesn't seem to be a straight forward way of doing this that I can find. So I figured I would use "try/except". But vw still throws an error 😕  symName = 'mySymbolName' try:    vs.Symbol(symName,0,0,0) except:    vs.AlrtDialog(symName+' doesn't exist in this drawing') Edited November 3, 2019 by Nik Quote Link to comment
twk Posted November 3, 2019 Share Posted November 3, 2019 You have to check whether the symbol 'name' exists in the drawing. Â You can check using vs.GetObejct(symName) to see whether it returns None or 0. Quote Link to comment
Nik Posted November 3, 2019 Author Share Posted November 3, 2019 (edited) should have been more clear... need to check if the symbol exists in the drawing symbol library. vs.GetObject works to check if a symbol has been inserted in a drawing. try/except does work for other types of errors. but maybe not for VW thrown errors?  The following bit of code works as expected  try:    x = 9/0 except:    vs.AlrtDialog('This is division by zero!')   Edited November 3, 2019 by Nik to clarify post Quote Link to comment
twk Posted November 3, 2019 Share Posted November 3, 2019 Oh ok. You mean in another file? or the current document? Quote Link to comment
Nik Posted November 3, 2019 Author Share Posted November 3, 2019 current document  Quote Link to comment
twk Posted November 3, 2019 Share Posted November 3, 2019 You'd have to use document list handling. Â Symbols are resources, so you'd build a resource list and cycle through the names in that list to see whether that symbol exists. Â http://developer.vectorworks.net/index.php/VS:BuildResourceList2 Â Â Quote Link to comment
PatW Posted November 4, 2019 Share Posted November 4, 2019 (edited) VS function's throw no exceptions. The errors are basically prints.  You can check a handle like a boolean to see if its valid: if not vs.GetObject(SymName): # XXX error # If another obj type could have the name this might be saver: kSymbol_Definition = 16 h = vs.GetObject(SymName) if (vs.GetTypeN(h) != kSymbol_Definition): # XXX error  Edited November 4, 2019 by PatW Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.