Jump to content
Developer Wiki and Function Reference Links ×

Script for replacement of symbols


Recommended Posts

Hi @ all,

 

I'm very often using the replacement button of the OIP for replacing Symbols. One of my permanent keyboard hacking  is to duplicate symbols and replace them. As far as I know there is no way to give the replacemnet button on the OIP a keyboard shortcode, so maybe a script can do this ?

In detail I want two small scrips:

 

1.) Opening  the replacement dialogue after having selected a Symbol

2.) Duplicating a selected symbol and opening the replacement dialogue afterwards.

 

or much better:

selecting  several symbols and replace the first chosen symbols with the last chosen one ....

 

As abslotue beginner in scripting, can someone give me a hint how to begin and what funtions are needed ? In the reference I can't find a direct function for replacement, so I gues I need some function that refers to the object info ? Or do I have to set two handles, on the new symbol and one on the symbols to be replaced and then somehow exchange them  ?

 

Thanks for help !

 

 

 

Edited by halfcouple
Link to comment

Hi,

 

you can assign a shortcut to the command Modify- > Convert- > Replace with Symbol... .

 

Quote

selecting  several symbols and replace the first chosen symbols with the last chosen one ....

 

this will not work. you can not check the order in which the symbols have been selected.

 

regards

 

Edited by Patrick Winkler
Link to comment

of course,  you can create your own command, this can easily cost you a day as a beginner.

Maybe it's less of an effort to just click on the button in the oip?

 

Do you need a dialog for selecting the new Symbol everytime?

If it's always the same Symbol you could write the name into an TextFile.

 

Here are some hints:

 

- Get the Sym location from the selected symbol instance:

http://developer.vectorworks.net/index.php/VS:GetEntityMatrix

 

- For the Sym Selection in the Dialog you need a Custom Thumb PopUp, I see if I can get an example tomorrow:

http://developer.vectorworks.net/index.php/VS:CreateCustThumbPopup

 

- Delete the instance with DelObject (handle)

 

- Replace it by the new Symbol:

http://developer.vectorworks.net/index.php/VS:Symbol

 

regards

Edited by Patrick Winkler
Link to comment
12 hours ago, Patrick Winkler said:

this can easily cost you a day as a beginner.

 

 

Yes, of course it will take me days  off desperately banging my head on the desktop, but it will take me one step further in learning Vectorscript, and that's the challenge... ;-)

So i f I understand it right the most complicated thing is to programm a thumb pop up and fill it with content ? Would be great it you find a sample for that.

 

Link to comment

It's actualy not as complicated as I rembembered:

 

Here is a python example that fills a ThumpPopUp with hatches:

def get_hatches ():
    '''
        Get a tuple with the name of all hatches in the actual Doc.
    '''
    hatch_nams = []
    
    num = vs.NameNum()
    
    for i in range (1, num + 1): # IMPORTANT: index must start at 1 here!
        name = vs.Index2Name(i)
        h = vs.GetObject(name)
        
        if vs.GetTypeN (h) == Obj_Type_Enum.hatch_defini:   # Here you have to check for Symbol-Definis (Type: 16)
            hatch_nams.append(name)
    
    return tuple (hatch_nams)



# Call this  function at the initialization of the dialog (Handler event: 12255)

def fill_hatch_popups ():
# Fill the Controls with all hatches in the doc
	for h_nam in get_hatches():
		vs.InsertImagePopupObjectItem(dialogID, kPD_Hatch_Hauptg, h_nam)


# You can read the selected value with:
hatch_hauptgeb = getImagePopupSelText(kPD_Hatch_Hauptg)

Also take a look at the example projects in the wiki: http://developer.vectorworks.net/index.php/Main_Page

 

regards

 

Link to comment

halfcouple,

   The code needed to do the symbol replacement is very short and easy –IF– you use the right call. As a seasoned Vector-Scripter I knew there was a call that would swap out the handles of one symbol definition with another, but it took me more than half an hour to find it. "SetHDef"  It's not listed with the Symbol routines, but rather with the Object Editing routines. I think it should be listed with both (and others), since it serves multiple object types, but that's another issue entirely.

 

   Using SetHDef will literally save you tons of work if you try to manually place a symbol were the old one was and then delete the original. If the existing symbols have Attached Records and/or Names, these will be preserved. The symbol's Position, Rotation, and Flip State are also preserved. And best of all, the original Stacking Order is preserved. If you try to preserve all of these attributes manually, your code will get pretty complicated quickly; and that doesn't include anything you need to do with the User Interface (UI). 

 

   Here's a short script that swaps all selected symbols with the one named at the top of the script. It has no UI – that's the hard part, and where you will spend most of your time writing scripts.

PROCEDURE ReplaceSymbols;
{ Replace selected Symbols with the symbol named in constant "NewSymName". }
{ This method preserves: Position, Rotation, Flip State, Name, Attached Records, Stacking Order, and Visibility. }
{ 03 Jun 2017 - Raymond J Mullin. }
CONST
	NewSymName = 'YourNewSymNameHere';		{ new sym - change as needed at the moment }
VAR
	NewSymDefH :Handle;


	function SwapSym(H :Handle) :Boolean;
	{ swap symbol instance H with NewSymDefH }
	Begin
		if (GetTypeN(H) = 15) then
			SetHDef(H, NewSymDefH);		{ swap Symbol Definitions }
	End;	{ SwapSym }


BEGIN
	NewSymDefH := GetObject(NewSymName);		{ Handle to new symbol name }
	if (NewSymDefH <> nil) then
		ForEachObjectInLayer(SwapSym, 2, 0, 4)	{ Selected, Shallow, Editable Layers }
	else AlrtDialog(concat('Symbol "', NewSymName, '" does not exist. Check spelling.')); 
	Sysbeep;					{ I'm done - tell everyone! }
END;
Run(ReplaceSymbols);

 

   As to crafting a UI, once you decide how your script/tool should run, i.e., whether you use a dialog to pick the new symbol, or pick your new symbol interactively AND whether you work on an existing selection or select symbols with your tool, you can write back for more advice on how to implement it. In the mean time play with this script and tweak the constants in the ForEachObjectInLayer() statement. There a quite a few variations in how that statement works. 

 

   To change the symbol that does the replacing, modify the symbol name in the CONST section at the top of the program. It's not elegant, but it works.

 

Good luck,

Raymond

 

 

 

 

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...