Start a new topic

newbie attribute questions - setting node ID

Original Post by: shawnallen Thu Apr 14 16:52:26 2016


Getting an indent error on this, not sure why:

db = mgGetCurrentDb()

selectList = mgGetSelectList (db)

num = mgGetRecListCount (selectList)


name_string = 'nameStringHere_'


if (num == 0):

mgSendMessage (MMSG_ERROR, "Nothing selected")

else:

mgDeselectAll(db)

for i in range (0, num):

rec,m = mgGetNextRecInList (selectList)

mgSelectOne(rec)

comment = mgGetComment (rec)

name = mgGetName (rec)

print 'Name: ', name



print 'Done'


Am trying to get selections, then set the ID for each to something.


Thx


Original Post by: shawnallen Thu Apr 14 19:50:26 2016


Cool, thanks. I didn't include any checks b/c the script was sooo simple :)


I'll get into good habits, I promise!

Original Post by: SteveThompson Thu Apr 14 19:48:13 2016


Oh, and to help you discover if the processing inside your loop is at fault, be aware that many of the OpenFlight Script functions return "status" values which will tell you whether or not the function succeeded. Again, it could be some function you are calling is "failing" but you are not paying attention to that and just assuming that the loop is not working properly.


For example, you call mgSelectOne. This function returns a value of type mgstatus. mgstatus is the opposite of boolean (don't ask me why it just is). If an object of type mgstatus is 0, it is a "good" status. A non-zero value is "bad". When you can, you should always write your code to at least receive the return value. You get extra credit for checking the return value (you should always IMO, but I get why people don't).


Anyway you could write the following into your script to "flag" a failure as follows:


s = mgSelectOne(rec)

if (not MSTAT_ISOK(s)): print 'mgSelectOne failed'


When you are not sure what is failing, you should start adding checks like this to help you isolate the problem. It's too easy to just say "the loop is not working" when really something inside is not working and you can find it fairly easily.


Welcome to software development, my friend!

Original Post by: shawnallen Thu Apr 14 19:39:00 2016


Excellent suggestion, thanks.


It seems the loop works fine by himself. I'll have to debug this when I need to rename a bunch of stuff again!

Original Post by: SteveThompson Thu Apr 14 19:32:21 2016


The code snippet does not in fact process all selected nodes. If I have 3 selected, it does 2. If I have (more), it leaves (3).

As a good detective, you might want to break this problem into 2 parts: the loop and the processing inside the loop.


First, check to verify the loop is "visiting" each node in the select list. You can verify this by looking at the output of the "print i" statement. If you see this 3 times (and note the way you wrote this you will see "0, 1, 2", not "1, 2, 3"), you are "visiting" each node in the list and you can assume your "loop" is working properly. If you only see this 2 times, you will know you are not "hitting" every node and you can figure out why the loop is bad. As a developer I always work this way. I will write my loop first and verify the loop is working before I continue. Here I might write this to start with:


db = mgGetCurrentDb()

selectList = mgGetSelectList (db)

num = mgGetRecListCount (selectList)


if (num == 0):

mgSendMessage (MMSG_ERROR, "Nothing selected")

else:

mgDeselectAll(db)

for i in range (0, num):

print i


print 'Done'


Then I will run this to verify I see the same number of print statements as I have selected. If I don't I know something is wrong with my loop. If I do see the proper number then I add code inside my loop.


Once you figure out the loop is working properly, then look at the processing inside the loop. You may be "hitting" each node in the select list, but something you are doing to that node is failing.


Conclusion: If you only look at the final expected results of your script and you see 2 nodes were "changed" but you expected 3, you really can't tell which part is broken.


Good luck, let us know what you find.

Original Post by: shawnallen Thu Apr 14 17:18:56 2016


Nope, no updates - this is what I'm running. Dang it!


I'm running this in v14, will try it in 15 and see what happens. Thanks for checking!

Original Post by: ChrisRogers Thu Apr 14 17:17:31 2016


I don't see the problem. It visits each node for me. Do you have an updated version of the script I can try? The last one you submitted works as expected

db = mgGetCurrentDb()

selectList = mgGetSelectList (db)

num = mgGetRecListCount (selectList)

name_string = 's_rdMark_std_aged_off_'


if (num == 0):

mgSendMessage (MMSG_ERROR, "Nothing selected")

else:

mgDeselectAll(db)

for i in range (0, num):

print i

rec,m = mgGetNextRecInList (selectList)

mgSelectOne(rec)

comment = mgGetComment (rec)

name = name_string + str(i)

#name = mgGetName (rec)

ok = mgSetName (rec, name)


print 'Done'

Original Post by: shawnallen Thu Apr 14 17:08:32 2016


Extra credit question -


The code snippet does not in fact process all selected nodes. If I have 3 selected, it does 2. If I have (more), it leaves (3).


I know it's a logic issue, but num == selected nodes, so why the discrepancy?? I mean, I understand starting with 0 and being off by one, but why isn't it always off by 1??

Original Post by: ChrisRogers Thu Apr 14 17:05:12 2016


lol yes. I think it duplicates the indentation of the line above (regardless of what it contains)

Original Post by: shawnallen Thu Apr 14 17:03:25 2016


:snake: Give that man a gold star!


Pressing Enter in the script editor inserts a couple tabs and a couple spaces into the front of the new line of code.

Sheesh!


Thank you!!

Original Post by: ChrisRogers Thu Apr 14 17:00:11 2016


Looking ok in the script editor is due to tabs mixed with spaces. The editor lets you see if it is a tab or space, but you need to turn on the option. Try turning on View Whitespace from the view menu. It helps me sort these issues out when they pop up. (usually from copy-pasting code)

Original Post by: shawnallen Thu Apr 14 16:58:22 2016


shades of memory. It looks OK in the script editor.

Am going to use a different editor.

Original Post by: shawnallen Thu Apr 14 16:57:43 2016


OK, that was dumb of me. Thx.


I get the same error with code that is properly formatted:

db = mgGetCurrentDb()

selectList = mgGetSelectList (db)

num = mgGetRecListCount (selectList)


name_string = 's_rdMark_std_aged_off_'


if (num == 0):

mgSendMessage (MMSG_ERROR, "Nothing selected")

else:

mgDeselectAll(db)

for i in range (0, num):

rec,m = mgGetNextRecInList (selectList)

mgSelectOne(rec)

comment = mgGetComment (rec)

name = name_string + str(i)

#name = mgGetName (rec)

ok = mgSetName (rec, name)



print 'Done'

Original Post by: ChrisRogers Thu Apr 14 16:56:03 2016


Your indent error is because your print statement is not at the same indent as the line above. Python uses indentation for structure. for instance, under an if statement, only the indented lines are included in the if. You exit the if statement by using a different indent.


so when python sees that you added two extra spaces on the beginning of your print statement it gets confused and doesn't know what to do.

Login to post a comment