Jump to content
The Hamma

'ocnt' referenced before assignment

Recommended Posts

global ocnt, vcnt, mcnt
ocnt = 0
vcnt = 0
mcnt = 0

def count(h4):
	ocnt += 1
	if vs.GetTypeN(h4) == 5:
		vcnt += vs.GetVertNum(h4)
	if vs.GetTypeN(h4) == 21:
		vcnt += vs.GetVertNum(h4)
	if vs.GetTypeN(h4) == 40:
		mcnt += 1

def GetHandles2(h1):	
	while h1 != []:
		hSEL.append(h1)
		if vs.GetTypeN(h1) == 92:
			h2 = vs.FInGroup(h1)
			while h2 != []:
				hSEL.append(h2)
				h2 = vs.NextObj(h2)
		h1 = vs.NextObj(h1)

def GetHandles(h5):	
	h2=vs.FInSymDef(h5)
	while h2 != []:
		hVP.append(h2)
		itParent = vs.GetTypeN(h2)  # Get Parent type
		if itParent == 11:
			h3 = vs.FInGroup(h2)
			while h3 != []:
				hVP.append(h3)
				h3 = vs.NextObj(h3)
		h2 = vs.NextObj(h2)

def DoIt(h5):
	ocnt = 0
	vcnt = 0
	mcnt = 0	
	if h5 != []:
		if vs.GetTypeN(h5) == 16:
			GetHandles(h5)
			for h4 in hVP:
				count(h4)
			vs.SetRecord(h5,'Obj Count')
			vs.SetRField(h5,'Obj Count','Obj Count',vs.Num2Str(0,ocnt))
			vs.SetRField(h5,'Obj Count','Vertex Count',vs.Num2Str(0,vcnt))
			vs.SetRField(h5,'Obj Count','Mesh Count',vs.Num2Str(0,mcnt))
			vs.symbol(vs.GetSDName(h5),xP,yP,0)
			xP = xP + 300
			if xP >= 5000:
				xP = 0
				yP = yP + 300
	xP =0
	yP =0

if vs.GetObject('Obj Count') == []:
	vs.NewField('Obj Count', 'Obj Count', '0', 4, 0)
	vs.NewField('Obj Count', 'Vertex Count', '0', 4, 0)
	vs.NewField('Obj Count', 'Mesh Count', '0', 4, 0)

hVP = []
hSEL = []
GetHandles2(vs.FSymDef())
for h5 in hSEL:
	if vs.GetTypeN(h5) == 16:
		DoIt(h5)

What am I doing wrong?

keep getting 'ocnt' referenced before assignment error. 

Share this post


Link to post

Thanks I have that working but now my counts and my symbol placements are off.  My counts keep adding to the last symbols count and each symbol is being placed dead center in the drawing.  They should be moving 300 to the right until 5000 is reached then move up 300 and so on.  

def DoIt(h5):
	global ocnt, vcnt, mcnt,reset
	ocnt = 0
	vcnt = 0
	mcnt = 0
	reset = 0
	xP = 0
	yP = 0	
	if h5 != []:
		if vs.GetTypeN(h5) == 16:
			GetHandles(h5)
			reset = 0
			for h4 in hVP:
				count(h4)
			reset = 0
			vs.SetRecord(h5,'Obj Count')
			vs.SetRField(h5,'Obj Count','Obj Count',vs.Num2Str(0,ocnt))
			vs.SetRField(h5,'Obj Count','Vertex Count',vs.Num2Str(0,vcnt))
			vs.SetRField(h5,'Obj Count','Mesh Count',vs.Num2Str(0,mcnt))
			vs.Symbol(vs.GetSDName(h5),xP,yP,0)
			xP = xP + 300
			if xP > 5000:
				xP = 0
				yP += 300
				
def count(h4):
	global ocnt, vcnt, mcnt,reset
	if reset != 1:
		ocnt = 0
		vcnt = 0
		mcct = 0
	ocnt = ocnt + 1
	if vs.GetTypeN(h4) == 5:
		vcnt = vcnt + vs.GetVertNum(h4)
	if vs.GetTypeN(h4) == 21:
		vcnt = vcnt + vs.GetVertNum(h4)
	if vs.GetTypeN(h4) == 40:
		mcnt = mcnt+ 1
	reset = 1

def GetHandles2(h1):	
	while h1 != []:
		hSEL.append(h1)
		if vs.GetTypeN(h1) == 92:
			h2 = vs.FInGroup(h1)
			while h2 != []:
				hSEL.append(h2)
				h2 = vs.NextObj(h2)
		h1 = vs.NextObj(h1)

def GetHandles(h5):	
	h2=vs.FInSymDef(h5)
	while h2 != []:
		hVP.append(h2)
		itParent = vs.GetTypeN(h2)  # Get Parent type
		if itParent == 11:
			h3 = vs.FInGroup(h2)
			while h3 != []:
				hVP.append(h3)
				h3 = vs.NextObj(h3)
		h2 = vs.NextObj(h2)

if vs.GetObject('Obj Count') == []:
	vs.NewField('Obj Count', 'Obj Count', '0', 4, 0)
	vs.NewField('Obj Count', 'Vertex Count', '0', 4, 0)
	vs.NewField('Obj Count', 'Mesh Count', '0', 4, 0)

hVP = []
hSEL = []
GetHandles2(vs.FSymDef())
for h5 in hSEL:
	if vs.GetTypeN(h5) == 16:
		DoIt(h5)

 

Edited by The Hamma

Share this post


Link to post

In def DoIt(h5): I am resetting the variables vcnt,  ocnt, and mcnt  to  0 but when it runs for h4 in hVP:  the variables return to their state the last time def count(h4): was triggered by  for h4 in hVP:.  I want  vcnt,  ocnt, and mcnt  to  be 0 every time  the for h4 in hVP: loop starts.   Help please. 

 

I am checking the state of the variable  at this point it the below script

            vs.SetRField(h5,'Obj Count','Obj Count',vs.Num2Str(0,ocnt))
            vs.AlrtDialog(ocnt) #Displays current count but for somereason is adding the previous count to the current count
            vcnt = ocnt = mcnt = 0 #reset to variable to 0
            vs.AlrtDialog(ocnt) #displays that variable has been reset to 0

 

reset = xP = yP = vcnt = ocnt = mcnt = 0

def count(h4):
	global vcnt, ocnt, mcnt
	ocnt += 1
	if vs.GetTypeN(h4) == 5:
		vcnt += vs.GetVertNum(h4)
	if vs.GetTypeN(h4) == 21:
		vcnt += vs.GetVertNum(h4)
	if vs.GetTypeN(h4) == 40:
		mcnt += 1			

def DoIt(h5):
	global reset,xP,yP, vcnt, ocnt, mcnt
	if h5 != []:
		if vs.GetTypeN(h5) == 16:
			GetHandles(h5)
			for h4 in hVP:
				count(h4)
			vs.SetRecord(h5,'Obj Count')
			vs.SetRField(h5,'Obj Count','Obj Count',vs.Num2Str(0,ocnt))
			vs.AlrtDialog(ocnt) #Displays current count but for somereason is adding the previous count to the current count
			vcnt = ocnt = mcnt = 0 #reset to variable to 0
			vs.AlrtDialog(ocnt) #displays that variable has been reset to 0
			vs.SetRField(h5,'Obj Count','Vertex Count',vs.Num2Str(0,vcnt))
			vs.SetRField(h5,'Obj Count','Mesh Count',vs.Num2Str(0,mcnt))
			vs.Symbol(vs.GetSDName(h5),xP,yP,0)
			xP += 300
			if xP > 5000:
				xP = 0
				yP += 300

def GetHandles2(h1):	
	while h1 != []:
		hSEL.append(h1)
		if vs.GetTypeN(h1) == 92:
			h2 = vs.FInGroup(h1)
			while h2 != []:
				hSEL.append(h2)
				h2 = vs.NextObj(h2)
		h1 = vs.NextObj(h1)

def GetHandles(h5):	
	h2=vs.FInSymDef(h5)
	while h2 != []:
		hVP.append(h2)
		itParent = vs.GetTypeN(h2)  # Get Parent type
		if itParent == 11:
			h3 = vs.FInGroup(h2)
			while h3 != []:
				hVP.append(h3)
				h3 = vs.NextObj(h3)
		h2 = vs.NextObj(h2)

if vs.GetObject('Obj Count') == []:
	vs.NewField('Obj Count', 'Obj Count', '0', 4, 0)
	vs.NewField('Obj Count', 'Vertex Count', '0', 4, 0)
	vs.NewField('Obj Count', 'Mesh Count', '0', 4, 0)


hVP = []
hSEL = []
GetHandles2(vs.FSymDef())
for h5 in hSEL:
	if vs.GetTypeN(h5) == 16:
		DoIt(h5)

 

Share this post


Link to post

I have to admit, I'm finding your script very hard to follow, which makes troubleshooting difficult. A couple general pieces of advice that may help:

 

- The advantage of breaking out functions from the main body is to have each function perform a unitary task. To that end, structure them in terms of taking an input and generating an output and should be testable on their own.

- To that end, avoid using global variables as a way to pass and retrieve values. Save them for things like constants or preferences. Your GetHandles functions should return an array of handles, which makes it clear what they do. Your count function can return a tupple of counts. That way, your main DoIt function can handle initializing, aggregating, and resetting variables and the data flow is clear.

- I'm getting lost with the *2, *3, *4… variants. Rename the variables to resemble what they are, like hInObject, hSymbol, hGroup, hObject. Don't worry about repeating variable names — unless you specify "global," each function has its own scope, which is another advantage of declaring a function.

 

Share this post


Link to post

I think the issue is that you just keep appending handles into hVP. As above, this is where using return values instead of globals would make this clear.

  • Like 1

Share this post


Link to post

 

On 2/12/2021 at 2:27 PM, JBenghiat said:

I think the issue is that you just keep appending handles into hVP. As above, this is where using return values instead of globals would make this clear.

Fixed it. Thanks!

ecnt = reset = xP = yP = vcnt = ocnt =scnt= mcnt= pcnt  = 0

def count(h4):
	global vcnt, ocnt, mcnt, ecnt,scnt,pcnt
	ocnt += 1
	if vs.GetTypeN(h4) == 5:
		vcnt += vs.GetVertNum(h4)
	if vs.GetTypeN(h4) == 21:
		vcnt += vs.GetVertNum(h4)
	if vs.GetTypeN(h4) == 25:
		vcnt += vs.GetVertNum(h4)
	if vs.GetTypeN(h4) == 40:
		mcnt += 1	
	if vs.GetTypeN(h4) == 24:
		ecnt += 1		
	if vs.GetTypeN(h4) == 84:
		ecnt += 1		
	if vs.GetTypeN(h4) == 95:
		ecnt += 1
	if vs.GetTypeN(h4) == 34:
		ecnt += 1		
	if vs.GetTypeN(h4) == 15:
		scnt += 1		
	if vs.GetTypeN(h4) == 86:
		scnt += 1	


def DoIt(h5):
	global reset,xP,yP, vcnt, ocnt, mcnt,scnt, ecnt,pcnt
	vs.ForEachObjectInList(count, 0, 2, vs.FInSymDef(h5))
	vs.SetRecord(h5,'Obj Count')
	vs.SetRField(h5,'Obj Count','Obj Count',vs.Num2Str(0,ocnt))
	vs.SetRField(h5,'Obj Count','Vertex Count',vs.Num2Str(0,vcnt))
	vs.SetRField(h5,'Obj Count','Mesh Count',vs.Num2Str(0,mcnt))
	vs.SetRField(h5,'Obj Count','3D Volume Count',vs.Num2Str(0,ecnt))
	vs.SetRField(h5,'Obj Count','Nested Symbol Count',vs.Num2Str(0,scnt))
	vs.SetRField(h5,'Obj Count','PIO Count',vs.Num2Str(0,pcnt))
	vcnt = ocnt = mcnt = ecnt =scnt =pcnt = 0 #reset to variable to 0
	vs.Symbol(vs.GetSDName(h5),xP,yP,0)
	xP += 300
	if xP > 5000:
		xP = 0
		yP += 300


if vs.GetObject('Obj Count') == []:
	vs.NewField('Obj Count', 'Obj Count', '0', 4, 0)
	vs.NewField('Obj Count', 'Vertex Count', '0', 4, 0)
	vs.NewField('Obj Count', 'Mesh Count', '0', 4, 0)
	vs.NewField('Obj Count', '3D Volume Count', '0', 4, 0)
	vs.NewField('Obj Count', 'Nested Symbol Count', '0', 4, 0)
	vs.NewField('Obj Count', 'PIO Count', '0', 4, 0)

hSEL = []
h1 =vs.FSymDef()
while h1 != []:
	hSEL.append(h1)
	if vs.GetTypeN(h1) == 92:
		h2 = vs.FInGroup(h1)
		while h2 != []:
			hSEL.append(h2)
			h2 = vs.NextObj(h2)
	h1 = vs.NextObj(h1)

for h in hSEL:
	if vs.GetTypeN(h) == 16:
		DoIt(h)

 

Share this post


Link to post

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


 

7150 Riverwood Drive, Columbia, Maryland 21046, USA   |   Contact Us:   410-290-5114

 

© 2018 Vectorworks, Inc. All Rights Reserved. Vectorworks, Inc. is part of the Nemetschek Group.

×
×
  • Create New...