Jump to content

Duplicate worksheets and put them in the right Folder


Recommended Posts

I am trying to duplicate two Worksheets, place them on a Layer and move it to the right worksheet folder.

 

I got it all working, except that the worksheets seem like they are not saved correct.

If I want to activate the placed worksheet image in the resource manager, it doesn't work.

 

Does someone have some experience ?

 

Thank you very much

 

# Script by Lukas Tobler | 27.04.2023
# Platziert die Tabellen automatisch auf allen Raumstudien und filtert den korrekten Raum

import vs

criteria_point = "(NOTINDLVP & NOTINREFDLVP & ((T=LOCUS) & (C='Hilfskonstruktionen')))"

def create_tab_definition(handle_ref_tab, story):

	try:
		vs.NameObject(f"RST_{story}")
		folder_handle = vs.BeginFolderN(18)
		ws_handle = vs.CreateDuplicateObject(handle_ref_tab, folder_handle)
		vs.EndFolder()
		return ws_handle
	except:
		folder_handle = vs.GetObject(f"RST_{story}")
		ws_handle = vs.CreateDuplicateObject(handle_ref_tab, folder_handle)
		return ws_handle

	# Pro Geschoss (UG04-OG04) einen Ordner erstellen mit dem Namen "RST_Geschoss" (zb "RST_UG03")
	# Falls Ordner schon vorhanden, dann den Handle von diesem Ordner wiedergeben
	
	# Kopie von Tabelle "Vorlage Räume" oder "Vorlage Türen" erstellen und im Ordner speichern
	
	

def main(point_handle):

	layer_handle = vs.GetLayer(point_handle)
	layer_name = vs.GetLName(layer_handle)
	
	if "RST" in layer_name:
	
		def get_ttb(h):
			global ttb_handle
			ttb_handle = h
		
		vs.Layer(layer_name)
		
		vs.ForEachObject(get_ttb, "((PON='Title Block Border') & (L='"+layer_name+"'))")

		story = layer_name[19:23]
		
		record_handle = vs.GetRecord(ttb_handle, 2)
		ttb_field = vs.GetRField(ttb_handle, record_handle.name, "Sheet Title")
		seperator_index =  ttb_field.find(" /")
		room_number = ttb_field[:seperator_index]

		insertion_point = vs.Get2DPt(point_handle, 1)
		
		handle_ref_tab = vs.GetTopVisibleWS()
		
		if handle_ref_tab.name == "Vorlage Räume":
			new_tab = create_tab_definition(handle_ref_tab, story)
			vs.SetWSCellFormulaN(new_tab, 1, 2, 1, 2, room_number)	
			vs.CreateWSImage(new_tab, insertion_point)
			vs.SetName(new_tab, room_number)
		else:
			new_tab= create_tab_definition(handle_ref_tab, story)
			insertion_point = (insertion_point[0], insertion_point[1]-0.185)
			vs.SetWSCellFormulaN(new_tab, 1, 2, 1, 2, room_number)	
			vs.CreateWSImage(new_tab, insertion_point)
			vs.SetName(new_tab, f"{room_number}_Türen")
		
		
vs.ForEachObject(main, criteria_point)

 

Link to comment

I noticed that in your code, the try and except block within the create_tab_definition function may not work as intended. Vectorworks won't actually throw an exception if the object name already exists, so the except block might never be executed.

Instead, you could check whether vs.NameObject(f"RST_{story}") returns a valid handle (i.e., not [0, None]) to determine if the object already exists. If it does, you can simply retrieve the handle and create the duplicate object within the existing folder. Here's an updated version of the create_tab_definition function with this check:

def create_tab_definition(handle_ref_tab, story):
    rst_handle = vs.NameObject(f"RST_{story}")
    if rst_handle != [0, None]:
        folder_handle = rst_handle[0]
    else:
        folder_handle = vs.BeginFolderN(18)
        vs.NameObject(f"RST_{story}", folder_handle)
        vs.EndFolder()
	
    ws_handle = vs.CreateDuplicateObject(handle_ref_tab, folder_handle)
    return ws_handle

 

 

The other thing that I havent tested, or come accross is using the vs.CreateDuplicateObject to duplicate resources. Does that actually work? I will try later.

 

 

Link to comment

Hi

 

Thanks for this neat solution. I have noticed that vw doesn't throw actual error, but thought I'm dealing with it later.

 

I got the vs.CreateDuplicateObject function working as following

vs.CreateDuplicateObject(h, vs.GetParent(h))

 

  • Like 1
Link to comment

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.

×
×
  • Create New...