Jump to content
Developer Wiki and Function Reference Links ×

Vectorscript to add a letter to an existing field


Recommended Posts

I need help creating a Vectorscript that will add a letter to a symbol number.  I have symbols created that I have numbered 1.001, 1.002 and so on.  I need a script or a quick way to add a letter to these numbers (1A.001, 1A.002 and so on.  I'd appreciate any ideas on how I can accomplish this.

Link to comment

How much scripting experience do you have?  Here is the general procedure:

- Get a handle to the first symbol definition: FSymDef

- Step through the symbols, either with NextObj or ForEachObjectInList

- Confirm that the current object is a symbol definition

- Get the name of each symbol

- If you're using VS, use Insert() to insert a character in the name.  Python has built-in string handling procedures

- Set the name of the symbol

 

This isn't quite what you want, but it processes symbol names:

PROCEDURE ChangeSymbolPrefic;
VAR
	newName	:STRING;
	oldName	:STRING;

{---------------------------------------------------------------------}
FUNCTION ChangeModelSym(h:HANDLE):BOOLEAN;
VAR
	symName:STRING;
	labelH:HANDLE;
	labelN:DYNARRAY[] OF CHAR;
BEGIN
	IF GetTypeN(h) = 16 THEN BEGIN
		symName:=GetName(h);
		IF Copy(symName, 1, Len(oldName))=oldName THEN BEGIN
			Delete(symName, 1, Len(oldName));
			Insert(newName, symName, 1);
			SetName(h, symName);
		END;
	END;
END;
{---------------------------------------------------------------------}
	
BEGIN
	oldName:='Old';
	newName:='New';
	ForEachObjectInList(ChangeModelSym, 0, 2, FSymDef);		
END;
Run(ChangeSymbolNames);

-Josh

Link to comment

I am new to Vectorscript, I have never written my own scripts but I have successfully edited existing ones to fit my needs.  I appreciate your answer but I think I may need to give you a clearer description of what I want to do.  I would like to be able to select a group of symbols and then run a script that would add a letter to the number field in my record format.  What I have is a value of 1.001 entered in the Number field for my symbol I need to be able to add a letter to that value without changing the numbers.  1A.001.  hopefully this is a better description of my needs.

Link to comment

The first issue is to make sure that your Number field is formatted as TEXT not as a Number. If it is a number type field they you will not be able to add a letter.

 

Also, do you want to add the letter to every object in the file that has the record? Only ones that have a number in the field? How do you want to make sure you don't accidentally add duplicate letters if one has already been added?

 

The script will not be hard. The outline if we use exactly what you have above would be:

 

Function AddLetter(Hd:Handle):String;

Var  S1,S2,S3:String;

        P1,Integer;

Begin

S1:=GetRField(Hd,YourRecord.YourField);

P1:=Pos('.',S1);

S2:=Copy(S1,1,P1-1);

S3:=Copy(S1,P1,(Len(S1)-P1);

AddLetter:=Concat(S2, 'Your character to add', S3);  {Change the middle string to whatever you want your letter to be}

 

Begin

  ForEachObjectInList(AddLetter, 2, 1, FSActLayer);  {Options set to do selected object on active layer including objects in groups}

End;

 

Please note that the script before was written in the browser and has not been tested and is not a complete script. Please try on dummy objects in a test file before attempting to use anything on your real data. ALWAYS make an extra backup copy before doing anything that is kind a a global replace just in case something goes wrong.

 

Write back if you need more help.

Link to comment

The first issue is to make sure that your Number field is formatted as TEXT not as a Number. If it is a number type field they you will not be able to add a letter.

My number field is formatted as text.

 

Also, do you want to add the letter to every object in the file that has the record?

I would like to add the letter to only symbols I have selected.

 

How do you want to make sure you don't accidentally add duplicate letters if one has already been added?

Duplicate letters will not be an issue, we are using this to label symbols in a certain area.  So symbols in one area will all have "A" and in another area they will have a "B"

 

Hopefully this clears this up, I appreciate the help.

Link to comment

OK, Try this:

 

Procedure AddIdentifierToField;

{©2017, Patrick Stanford, pat@coviana.com  Licensed under the LGPL}
{No Warranty Expressed or Implied. Test thoroughly before using on production data}
{Make a backup copy before running}
{May change data in hidden but selected objects in groups, symbols, etc.}
{Do no operate heavy machinery while using. May cause drowsiness}

var AddedString:String;

Procedure AddLetter(Hd:Handle);
Var  S1,S2,S3:String;
        P1:Integer;
Begin
	S1:=GetRField(Hd, 'MyRecord', 'MyField');
	P1:=Pos('.',S1);
	S2:=Copy(S1,1,P1-1);
	S3:=Copy(S1,P1,(Len(S1)-P1+1));
	SetRField(Hd, 'MyRecord', 'MyField', Concat(S2, 'A', S3));  {Change the middle string to whatever you want your letter to be}
End;

Begin
  ForEachObject(AddLetter, (((R IN ['MyRecord']) & (SEL=TRUE))));  {Options set to do selected object on active layer including objects in groups}
End;

Run(AddIdentifierToField);

Change ever occurrence of MyRecord and MyField in the script to the names of your record and field. Change the letter in the SetRField line to what you desire.

 

Let me know if you need more help.

Link to comment

Try this one.

 

Procedure AddIdentifierToField;

{©2017, Patrick Stanford, pat@coviana.com  Licensed under the LGPL}
{No Warranty Expressed or Implied. Test thoroughly before using on production data}
{Make a backup copy before running}
{May change data in hidden but selected objects in groups, symbols, etc.}
{Do no operate heavy machinery while using. May cause drowsiness}

{Updated March 7, 2017 to add dialog box to get character(s) to add}

var MyLetter:String;

Procedure AddLetter(Hd:Handle);
Var  S1,S2,S3:String;
        P1:Integer;
Begin
	S1:=GetRField(Hd, 'MyRecord', 'MyField');
	P1:=Pos('.',S1);
	S2:=Copy(S1,1,P1-1);
	S3:=Copy(S1,P1,(Len(S1)-P1+1));
	SetRField(Hd, 'MyRecord', 'MyField', Concat(S2, MyLetter, S3));  {Change the middle string to whatever you want your letter to be}
End;

Begin
  MyLetter:=StrDialog('Enter Characters to insert', 'A');
  ForEachObject(AddLetter, (((R IN ['MyRecord']) & (SEL=TRUE))));  {Options set to do selected object on active layer including objects in groups}
End;

Run(AddIdentifierToField);

 

Link to comment

If you are going to the Design Summit in Baltimore in October, you might want to make travel arrangements to stay through Thursday and leave Friday (hint, hint).

 

Otherwise, there are no great training resources. Kind of the best are to search the threads here for scripts that are close to what you want and then ask questions.  The documentation is mostly available online at developer.vectorworks.net.  I prefer the local version of the function reference that can be found in your program folder at Vectorworks 2017/VWHelp/Script Reference

 

 

Link to comment

I second Pat's advice.  I'm not nearly as advanced as others, but I can write a mean script.  I learned it all from starting with something very simple, looking at other scripts and parsing how they worked, and spending a LOT of time looking at the function reference document.  The function reference document has examples for many of the functions, and that will give you ideas as you go.

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