Jump to content
Developer Wiki and Function Reference Links ×

giving object a unique name/number


Recommended Posts

how can I create a script that develops an object of a certain type and class and automatically gives it a name and number. The name I want via info palette as a prefix type, followed by a sequential number. Of course there should also be an option to reset the number so that I can add again to number.

 

Thanks for the help

Pascal

Link to comment

Hello Pat

 

Thanks for the help you want to give me.

I try to describe what I want.

 

It's actually about making a measurement statement for 2d drawings. In the facade I now draw surfaces for the different facade elements with rectangles. I now manually enter a name for these rectangles via the PIO and then I can retrieve the name of that rectangle and the dimensions to retrieve the m2 via spreadsheet. So now I have to draw the rectangle every time and then manually enter a name. So I wondered if I could create that name by, for example, a kind of stamp. But then a stamp that is immediately linked to that rectangle. Then I don't have to do that manually for every rectangle. The order of the numbering is actually determined according to the click on the rectangle or object that I click. I hope this is clear?

 

Pascal

Link to comment

Here is a script with the basics but It works and should give you some ideas on how to improve it.

 

The biggest thing it needs is a way to check that a name is not already in use as VW will only allow a single object to have a name. Also, as is it only does the number not the prefix.

 

If you hold down the Command key when you click on an object it will ask you for the number to give that object and then start incrementing from there for the next clicked object.

 

Ask again if you need more help.

 

Procedure PickAndName;

Var	H1:	Handle;
	X1,Y1,Z1, X2,Y2: Real;
	N1: Integer;
	B1, B2: Boolean;
	
Function CallBack(Hd1:Handle):Boolean;
	Begin
		If GetType(Hd1)=3 then CallBack:=True;
	End;
	
Begin
	N1:=1;
	B2:=False;
	While B2 = False do
		Begin
			TrackObjectN(0, CallBack, H1, X1,Y1,Z1);
			If H1 <> Nil then
				Begin
					If Command then N1:=IntDialog('Enter Number for this Object.', Num2Str(0,N1));
					SetName(H1,Num2Str(0,N1));
					N1:=N1+1;
				End
			Else	B2:=True;
		End;
End;

Run(PickAndName);

 

  • Like 1
Link to comment

hello pat

Thank you very much for the script you made for me. It looks very good and is already moving in the direction I want. But I still had a few questions.

1. I now see that it only works on rectangles, can this also be extended to other 2d shapes?
2. Could there be a field somewhere where I could enter a prefix, followed by the automatic number?

3. Suppose I have 2 shapes whom I want to call "wall" 1 and "wall" 2.
Then I want to number further with "window" 1 and "window" 2. How can I reset the numbers?

If these things were possible, I would have a very strong script for making my measurement statements for the drawings that are still drawn in 2d.

Thanks again for the help.
Pascal

Link to comment

Yes it can be extended to other shapes.

 

GetType(Hd1)=3. is the part that defines the object type. You can OR these together to get what you want.  ((GetType(HD1)=3) or (GetType(HD1)=4)) will highlight over Rectangles and Ovals (circles).  See the Vectorscript Appendix (click the link at the top of the page or look in your Applications Folder:Vectorworks 2020:VW Help: Script Reference) for other object types.

 

You can certainly add a prefix. Just have to define it.  Something like SetName(H1, Concat('TisMacFan-',Num2Str(0,N1)));  You could store it in a variable and CONCAT the variable instead of hard coding it. Or you could create a custom dialog box (or use two dialog boxes) to allow you to enter the prefix the same way you enter the number holding down the command key.

 

Hold down the Command Key when you click on an object and it will ask for the Number for the next object.

 

HTH

Link to comment

Hello Pat

 

The first thing to select different shapes has I managed with your tip, but the second

"You can certainly add a prefix. Just have to define it.  Something like SetName(H1, Concat('TisMacFan-',Num2Str(0,N1)));  You could store it in a variable and CONCAT the variable instead of hard coding it. Or you could create a custom dialog box (or use two dialog boxes) to allow you to enter the prefix the same way you enter the number holding down the command key."  I don't understand how to start with it.  

I like to work with a variable, or with to dialogboxes

So I hope you can help me a little bit further with this part of the script

Pascal

Link to comment

This version includes the ability to add a prefix.

 

If you hold down the Command Key when you click on a highlighted object, you will not get two dialog boxes, the first will ask for the number to use for that object, the second will ask for the Prefix to use. The prefix will then be used for all future objects until you change it.

 

The number is reset to 1 each time you run the script. The prefix is reset to nothing (empty string) each time the script is run.

 

Procedure PickAndName2;

Var	H1:	Handle;
	X1,Y1,Z1, X2,Y2: Real;
	N1: Integer;
	B1, B2: Boolean;
	S1: String;
	
Function CallBack(Hd1:Handle):Boolean;
	Begin
		If GetType(Hd1)=3 then CallBack:=True;
	End;
	
Begin
	N1:=1;
	B2:=False;
	While B2 = False do
		Begin
			TrackObjectN(0, CallBack, H1, X1,Y1,Z1);
			If H1 <> Nil then
				Begin
					If Command then 
						Begin
							N1:=IntDialog('Enter Number for this Object.', Num2Str(0,N1));
							S1:=StrDialog('Enter Prefix for String', S1);
						End;
					SetName(H1,Concat(S1,Num2Str(0,N1)));
					N1:=N1+1;
				End
			Else	B2:=True;
		End;
End;

Run(PickAndName2);

 

Link to comment
  • 2 months later...
  • 8 months later...

Try something like this:

 

This was shamelessly stolen and converted to TrackObjectN based on a script by @Jesse Cogswell posted in this thread back in July of 2020.

 

If you change the first value in the TrackObjectN call to a 2 it will not find objects in walls, only directly on the design layer.

 

PROCEDURE GetDoorWindow;
VAR
    trackHand:HANDLE;
    trackLoc:POINT3D;
FUNCTION CheckObjCallback(h1:HANDLE) : BOOLEAN;
{Provides Callback for selecting Doors and Windows inserted into Walls}
        
    BEGIN
 If ((GetTypeN(h1) = 86) and ((GetName(GetParametricRecord(h1))='Door') OR (GetName(GetParametricRecord(h1))='Window'))) then CheckObjCallback:=True;     
    END;

BEGIN
    TrackObjectN(1,CheckObjCallback,trackHand,trackLoc.x,trackLoc.y,trackLoc.z);
END;
Run(GetDoorWindow);

 

Link to comment
  • 1 year later...

Hi @Pat Stanford

 

This has been super helpful to me, thank you so much for providing it!

 

I have managed to edit the script (using your advice) to include all the different object types I would need. is there a way to click on one object, assign it a number in the pop up box and then every other object/polygon takes its number from there? This would save me from having to click on each object. I have tried to see if there is a way to select all objects and then run the script but it still requires me to go through and click each one individually. 

 

I am wondering if there is a way of me selecting all objects, running the script, assigning the first ID number, and then for the script to automatically "click" through each selected object.

 

Hopefully this makes sense, I'm useless with scripting so any help would be appreciated!

Edited by loic.jourdan
Read over Pat's info again and realised how to change the selectable objects. :)
Link to comment
  • 5 weeks later...

I found this thread trying to do something like the OP wanted to do. I want to be able to modify a field in a PIO so I modified the main chunk of code to be -

If Command then N1:=IntDialog('Enter Number for this Object.', Num2Str(0,N1));
SetRField(H1, 'Roadcase by Mike', 'Piece Number', ConCat(N1)); 
ResetObject(H1);
RedrawSelection;

 

It works but I'd like to improve it with two options.

1 - I'd like the PIO to refresh/redraw to reflect the new Piece  Number while the tool is still active to make it easier to keep track of what has been numbered already.

2- Would it be possible to store the value of N1 when the tool exits to be able to resume numbering from the same place?

 

I've been able to make some solid strides in my coding so any bread crumbs that I can follow up with would be great.

Link to comment

Unfortunately, I don't think that you will be able to fulfill your first request.  From my understanding, due to the modal nature of VW, the reset and redraw won't actually register until your script completes.  At least I've never been able to achieve what you are looking for.

 

As for the second request, look into the Rpstr_SetValueInt and Rpstr_GetValueInt functions.  They will do exactly what you are looking for, and will persist from one file to another.  If you want it to reset when you run the command in a different file, you can use Rpstr_SetValueStr and Rpstr_GetValueStr to store the name of the currently open file.  If the current filename (retrieved using GetFPathName) doesn't match what's stored in the string repository, you can use Rpstr_RemoveValue on the integer value to reset it back to its default (presumably 1), or just manually reset it in the dialog since the new value will be saved when you press OK anyway.

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