Jump to content
Developer Wiki and Function Reference Links ×

Cleaning up my first PIO - Catching Ghosts


Recommended Posts

PROCEDURE DrawRectangle;
VAR
    Width, Height, CenterX, CenterY: REAL;
    X1, Y1, X2, Y2: REAL;
    RectHandle: HANDLE;
	TextValue: STRING;
    TextHandle: HANDLE;
    MaxTextWidth: REAL;
    ClickX, ClickY: REAL;

BEGIN
    { Set your desired values for Width, Height, and Center coordinates }
    Width := PWidth;
    Height := PHeight;
   { GetPt(ClickX, ClickY);} {line commented out, didn't do what I wanted}


    { Calculate the coordinate s of the corners of the rectangle }
    X1 := ClickX - Width / 2;
    Y1 := ClickY - Height / 2;
    X2 := ClickX + Width / 2;
    Y2 := ClickY + Height / 2;

    { Create the rectangle using the calculated coordinates }
    Rect(X1, Y1, X2, Y2);
    
   
    
    TextSize(PText_size); { Adjust the size as needed }
    
    TextJust(2);
    CreateText(PText); 
	TextHandle := LNewObj;
	SetTextWrap(TextHandle, TRUE);
    MaxTextWidth := X2 - X1;
    SetTextWidth(TextHandle, MaxTextWidth);
         
    { Redraw the screen to see the changes }
    RedrawAll;
END;

{ Call the procedure to execute the script }
RUN(DrawRectangle);

 

I made a simple PIO to learn the basics of them. I used Chat GPT to get the code roughed in and looked at the vectorscript function reference to fill in the gaps. As a point plug in it works, it makes a rectangle where height, width, text size and text is set from the object info pallet . However I feel like it could be cleaned up a little. The first thing I would like to improve is the creation of 'ghosts' When activating the tool there is a ghost rectangle created at the origin and a dashed version around the cursor as you would expect. There is also a ghost at the origin when you edit any of the values in the object info pallet. Adjusting the view in any way seems to get rid of the ghost but I'd like to prevent them in the first place.

I looked at another script and saw code like 

	kObjOnInitXProperties				= 5;
	kObjOnWidgetPrep 					= 41;	
	kObjXPropShowOthersEditGroup		= 24;
	kObjXPropAcceptStates				= 18;
	kObjOnAddState 		    			= 44;
	kObjXPropEditGroupPath 				= 2;

Where can I learn more about those lines? They don't come up with googling or in the VS Ref...

Edited by Mike Rock
  • Like 1
Link to comment

My experiments with ChatGPT and Vectorscript always return VERY verbose code at the very best.  Often ChatGPT hallucinates functions.  And it can't handle plug-in objects very well - if at all.

 

The good news is you need MUCH less code than you think.  That's the beauty of writing plug-in objects.

 

It can't handle the plug-in definition at all.  And I'm not sure from your example that you are defining the plug in. 

 

 

If you want to make a plug-in object that draws a square of parametric values using a point plug in:

In Vectorworks, go to Tools>Plug-ins,>Plug-in Manager…

Chose New.  

Give it a name and select Point Object.

In the parameters tab do this:

image.thumb.png.83d5334fe032fa34c5b8a140ed73f59b.png

The name in the Name column is what you use in the script (with a p prefix).  The Alternate Name is what appears in the OIP and in a worksheet.

 

Believe it or not, this is the entire script you will need:

 

PROCEDURE ParametricRectangle;

BEGIN
	RectangleN(	0,0,
			1,0,
			pRectWidth,pRectHeight);
END;

RUN(ParametricRectangle);

 

 

pRectWidth and pRectHeight will be the values in the OIP for Rectangle Width and Rectangle Height!  Changing the values in the OIP (or in a worksheet) will change the size of the rectangle.

 

RectangleN is a standard way of drawing a rectangle.  The first two numbers are the starting point.  With plug in objects the insertion point of the object is 0,0.  The next two numbers define the direction of the base of the width of the rectangle.  1,0 means it goes to the right and not up or down.  -1,0 goes to the left.  1,1 goes at a 45º angle up and to the right.  The actual values don't matter.  Just if they are negative, zero, or positive.  The last numbers are the opposite corner of the rectangle.

 

You don't have to write them as 3 lines.  I just find that easier to read.

 

 

 

If you want to make a plug-in object that draws a square with handles at the corners:

In Vectorworks, go to Tools>Plug-ins,>Plug-in Manager…

Chose New.  

Give it a name and select Rectangle Object.

For a Rectangle Plug-in Object it will automatically add the width and height parameters!

So, without even lifting a finger the parameters will look like this:

image.thumb.png.eff2ae73fc8d8bd7f800c364213b7e19.png

The script will be something like this (I added diagonal lines just to give it something to do :-):

PROCEDURE RectRect;

BEGIN

	RectangleN(
		0,(0-(pBoxWidth/2)),
		1,0,
		pLineLength,pBoxWidth);

	MoveTo(0,-pBoxWidth/2);
	LineTo(pLineLength,pBoxWidth/2);

	MoveTo(0,pBoxWidth/2);
	LineTo(pLineLength,-pBoxWidth/2);

END;

RUN(RectRect);

 

With this plug-in you can either drag the handles of the rectangle or type in values in the OIP.

 

Note that this plug-in will have two insertion modes.  The second one is the one that makes sense.  But I don't know any way to make that the default  :-).

 

In the second insertion mode the linelength is the first line you draw and the boxwidth is what a human would think of as the height.

 

For reasons someone may soon tell us, but unknown to me for many years, the boxwidth (height) is measured from the middle of the rectangle.  So you have to do the division by 2 business.

 

I'll attach these two plug-in objects so you can take them apart and see how they work.  Let me know if you need help installing them.

 

Post back with any questions.

 

Welcome to a lifetime of madness.

 

 

 

EDIT:  Should have mention in directions above.  After you create the plug-in object you still need to add it to a workspace so it will show up in a tool palette or menu.

 

 

Rect Rect Example.vso Rect Point Example.vso

  • Like 3
  • Love 1
Link to comment

The kobj… lines are constant definitions.  Later on in the code the coder will want to use the integer value for OnWigetPrep and rather than look it up or remember a hundred of these numbers they are defined at the beginning of the code in the CONSTANT section. 

 

The functions that require those values are used when the plug-in object is "event aware".  Event aware plug-ins can do things like have Styles that are recalled from a stored symbol in the resource manager.

 

They don't show up in the reference because they aren't functions.  They are a common practice.  Not part of the language.  It's entirely possible that we are all copying that from @Julian Carr and @JBenghiat.

 

Don't worry about that for a while.  

 

You will have a few years of joyfully improving your productivity by making useful tools and menu commands before it seems like a good idea to subject yourself to that torment.  Maybe by then ChatGPT will be able to write plug-ins for us :-).

  • Love 1
Link to comment

Thanks @michaelk The simple is better approach seemed to help, oddly the redrawall line seemed to be causing the ghosts more than anything.

Your comment on kobj stuff is funny, I found it in a PIO and then also caught bits of it in the VS:REF. Picture me at my keyboard like Indiana Jones franticly googling for anything related to kobj sure it was the secret code I needed. Not knowing it was just a bit of copied code that had made its way around.... I should have paid more attention to the fact it was a constant.

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