Jump to content
Sign in to follow this  
Tom4

Script to draw a box with text in it

Recommended Posts

This is simple but, I hope it helps some new users get started.

PROCEDURE Note1;

VAR

Note : STRING;

P : REAL;

pX, pY:REAL;

Name:STRING;

Result:BOOLEAN;

gridDistance:REAL;

xValue, yValue: REAL;

gridDistance2:REAL;

TextHd:HANDLE;

verticalAlignment:INTEGER;

scaleXR,scaleYR:REAL;

h:HANDLE;

LScale:REAL;

BEGIN

SetOriginAbsolute(0,0);

{ NameClass('Symbols');

Layer('Symbols');

LScale:=(1/1);

SetScale(LScale);

gridDistance:= .5;

GridLines(gridDistance);

gridDistance2:= .125;

PenGrid(gridDistance2);

}

BeginGroup;

Note := ('Enter your text

Here.');

BEGIN

FillBack(0);

FillPat(1);

PenSize(7);

PenPat(2);

END;

TextFont(GetFontID('Arial'));

TextVerticalAlign(1);

P := 0.0;

pX := 0.0;

pY := 0.0;

GetPt( pX, pY);

MoveTo(pX,pY);

RELATIVE;

Rect(-0.5, -0.875, 0.5, 0.0);

RELATIVE;

TEXTJUST (2);

TEXTSIZE (10);

TEXTORIGIN (-0.0, -0.063);

BEGINTEXT; Note

ENDTEXT;

P := 0.0;

pX := 0.0;

pY := 0.0;

EndGroup;

DSelectAll;

END;

RUN (Note1);

Share this post


Link to post

Tom, here are some suggestions on this script:

1. I'd (just for clarity's sake) eliminate the commented out code.

2. I would eliminate the SetOriginAbsolute() call. No sense in changing the user's settings unless that is the purpose of the script.

3. The Note variable can easily be set to a user prompt with the StrDialog call, e.g.:

Note := StrDialog('Enter the boxed text:','ABC');

4. The fixed size box seems a little too restrictive. You could use a pair of x,y values and the GetRect() call to have the user enter the box using his cursor. Instead of:

GetPt( pX, pY);
MoveTo(pX,pY);
RELATIVE;
Rect(-0.5, -0.875, 0.5, 0.0);

You could more simply use:

GetRect(x1,y1,x2,y2);
Rect(x1,y1,x2,y2);

and you'd additionally have the advantage of a "rubberband" rectangle. You could use that same xy pair to locate the origin of the text at the center of the box and then you could dispense with all the RELATIVE() calls. In addition, you could set your text origin to the exact center of the text by adding a SetTextVerticalAlign() call, and then you wouldn't have to do the little vertical offset:

TextOrigin(x1+(x2-x1)/2,y1+(y2-y1)/2,);
BeginText;
note
EndText;
SetTextVerticalAlign(LNewObj,3);

I use the handle-based SetTextVerticalAlign() because I don't want to change the document settings.

As for the text size and font, I wouldn't specify them in the code. If you leave them unspecified, the script will use document defaults, and the user has more flexibility.

I think this illustrates that even in a simple script, there's room for thought and for the user experience. I'd likt to encourage you to take another pass at this script and really "tighten it up". Good luck!

Share this post


Link to post

Thanks Robert your comments are greatly appreciated I will take your advice. This forum is a great addition esspecially when the feed back is so possitive and helpful.

Thanks again

Share this post


Link to post

I've modified the script to your sugestions Robert, works great thanks. Alot of the predefined parameters were to stop errors as certain types of drawings have thier own standards, but the versatility of this should be very useful.

Tom

PROCEDURE Note1;

VAR

Note : STRING;

P : REAL;

pX, pY:REAL;

Name:STRING;

Result:BOOLEAN;

x1, y1: REAL;

x2, y2: REAL;

TextHd:HANDLE;

verticalAlignment:INTEGER;

h:HANDLE;

BEGIN

BeginGroup;

Note := StrDialog('Enter the boxed text:','ABC');

BEGIN

FillBack(0);

FillPat(1);

PenSize(7);

PenPat(2);

END;

TextVerticalAlign(3);

GetRect(x1,y1,x2,y2);

Rect(x1,y1,x2,y2);

RELATIVE;

TEXTJUST (2);

TEXTSIZE (10);

TextOrigin(x1+(x2-x1)/2,y1+(y2-y1)/2);

BEGINTEXT;

Note

ENDTEXT;

SetTextVerticalAlign(LNewObj,3);

EndGroup;

DSelectAll;

END;

RUN (Note1);

Share this post


Link to post

Robert, is there a way of modifying the script so the 'box' automatically resizes to 'fit' the text?

Share this post


Link to post

Mike,

There is. You can query the width of the text object - but I think in most cases the user wants the text width to be adjusted to the box width.

(It is funny that 20 year old cheap/free drafting programs we were able to put a box around a text object, but in VW, we have to have a Program for such a trivial thing.)

Share this post


Link to post

Mike, you can draw a piece of text and then use the GetBBox() call to determine its bounding box, to which you can then draw a rect.

Tom: Excellent improvement! Note that your new script is not only much shorter and easier to read, but also much more functional.

NB: you don't need the TextVerticalAlign() call, because you are using the SetTextVerticalAlign() call. They do the same thing in this case, and the latter call is better because it doesn't change the program settings. In like manner, you could use the SetTextJust() call instead of the TextJust() call.

My only other question now is the DSelectAll call. Wouldn't you run that (if at all) at the beginning, but leave selected the stuff you've just made? Then this works like any VectorWorks tool.

Share this post


Link to post

Robert, I deselect because when I go thru and am at the point to add text to my drawing I've ended up adding alot of text and it all stayed selected and when I would zoom in and move a text line other text would still be selected and move unknown so I just select the text I want to move and I know that's the only change I am making

Thank you all for the help on this

Tom

Share this post


Link to post

Good morning!

In celebration of being able to post files, here is a simple PIO that places text in automatically-resizing boxes.

(This is in reply to the discussion a while ago about scripting that process...)

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

 

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.

×