Jump to content
Developer Wiki and Function Reference Links Read more... ×
Sign in to follow this  
IanH

Creating poly with 2 vertices doesn't appear to work

Recommended Posts

I've written a script that basically joins up points of objects - but like dot to dot.

However, if there are only 2 vertices, I appear to be getting no poly created.

The documentation states that "A minimum of two vertices must be created" which I am doing - I'm effectively creating a single line in this case.

I wonder if anyone could quickly eyeball the example code extract to see if there is anything obvious (like a basic misunderstanding) that I am missing - I would hate to have to write a special case to handle just 2 vertices.

VW11 SP3.

IF objCount < 2 THEN
	AlrtDialog( 'Two or more objects need to be selected' )
ELSE
BEGIN
	BeginPoly;
	OpenPoly;
	Smooth( 0 );
	FOR obj := 1 TO objCount DO
	BEGIN
		hand := listOfObjs[ obj ].h;

		Addpoint( x, y );
alrtdialog( concat( 'Add point ', num2str( 3, x ), ', ', num2str( 3, y )));

	END;
	EndPoly;

	AlrtDialog( Concat( Num2str( 0, objCount ), ' objects joined' ));
END;

There is a pre populated array, listOfObjs, which holds objCount handles of the objects that I am joining. In the case that does not work, there are two objects and this confirmed by the debug dialog correctly displaying the coords of the two added points. Just when the script finishes, there is no line/poly created. It works fine for 3 or more vertices.

Thanks

Ian

Share this post


Link to post

Ian,

???I don't know of a direct way to create a Polygon or a Polyline with 2 vertices. Manually, you cannot draw only 2 vertices, as it will become a Line. If you draw 3, you cannot delete one vertex if it is a Polygon. However, you can delete one vertex if it is a Polyline (go figure).

???The backdoor approach (via VS) is to create a 3 point Poly (-gon or -line) and then delete a vertex.

This works to produce a 2 vertex Polygon:

BeginPoly;

??Addpoint( 1, 2 );

??Addpoint( 3, 4 );

??Addpoint( 3, 4 ); { Dummy point }

EndPoly;

DelVertex(LNewObj, 3);

???Your best option (meaning one that will be supported in all future versions of VW) is to create a Line when you have 2 vertices and a Poly when you have 3 or more. If you absolutely need it to be a Poly, then try the above method.

Good luck,

Raymond

PS - Please avoid One-Point-Polys. They will confuse everybody. ;-)

Share this post


Link to post
I don't know of a direct way to create a Polygon or a Polyline with 2 vertices. Manually, you cannot draw only 2 vertices, as it will become a Line.

That's interesting, because I can manually create a polyline with 2 vertices and it remains a polyline.

However, looks like I may be confusing the poly stuff which, as you say, seems to create a polygon not a polyline.

So, how do I mimic a polyline in VS? Looks like the things that works on polylines need handles to objects.

Sorry for it appearing that i'm not putting too much thought into this, but its late Friday and I only just logged in to confirm the 2 vertex polyline bit.

Share this post


Link to post
That's interesting, because I can manually create a polyline with 2 vertices and it remains a polyline.
I guess if I used the right tool it would have been easy. As you say, it's Friday and not all brain cells are on task.

VS, to the best of my knowledge (which is obviously limited), does not have a straightforward way to generate 2-vertex Polylines. Even the 2-vertex Polyline that you can draw manually does not export as VS such that you can re-import it and get back to where you started.

However, there are usually circuitous paths around most obstacles. I stumbled on this one only moments ago.

BeginPoly;

????MoveTo(1, 2);

????Lineto(3, 4);

????Lineto(3, 4);???{ Dummy vertex }

EndPoly;

SetVertexVisibility(LNewObj, 1, FALSE);???{ Makes it a Polyline }

Just duplicate the last vertex and hide a side. You'll end up with one less vertex and no hidden sides ( just as you'd surmise :-p ). If it weren't for guessing, programming VS wouldn't be any fun.

If you want a Polygon instead, then delete the third vertex as shown in my previous post instead of using SetVertexVisibility().

HTH,

Raymond

Share this post


Link to post

Thanks Raymond

Was hoping for something simple as the documentation says "A minimum of two vertices must be created" which would, to me, imply it could be done.

I will probably just put in a special case when I only have 2 vertex and create a simple line.

Thanks for the code BTW. I was not aware of LNewObj. That may well prove to be quite handy.

Share this post


Link to post

You're welcome, Ian. If you don't mind adding functions to your code, as in breaking from inline coding, try this one. Calling it will simplify the main loop keeping the hard part of your code more readable and concise. If you don't want or need the handle to the Poly, then you can redefine the function as a procedure.

Raymond

	function TwoPtPoly(P1, P2 :Point; Polygon :Boolean) :Handle;
{ Create a 2-vertex polygon if Polygon is TRUE, or a 2-vertex polyline if Polygon is FALSE. }
{ Return a handle to the new object. }
Begin
	BeginPoly;
	    MoveTo(P1.x, P1.y);
	    LineTo(P2.x, P2.y);
	    LineTo(P2.x, P2.y);  			{ Dummy vertex }
	EndPoly;
	if Polygon then DelVertex(LNewObj, 3)  		{ Makes it a Polygon }
	else SetVertexVisibility(LNewObj, 1, FALSE);	{ Makes it a Polyline }
	TwoPtPoly := LNewObj;
End;		{ TwoPtPoly }

BEGIN
OpenPoly;
Smooth( 0 );
IF ( objCount < 2 ) THEN
	AlrtDialog( 'Two or more objects need to be selected' )
ELSE IF ( objCount = 2 ) THEN
	H1 := TwoPtPoly(P1, P2, True)	{ TRUE = polygon, FALSE = Polyline }
ELSE BEGIN	{ 3+ objects }
	BeginPoly;
		FOR obj := 1 TO objCount DO
			...
	EndPoly;
END

END;

Share this post


Link to post

Thank very much for your assistance Raymond.

In the end, for the special case of only 2 vertices, I simply drew a line between start and end then converted to polyline with the aid of the very useful LNewObj function making it a cinch. Oddly enough, it created a 2 vertex polygon!

Thanks for the pointer of adding functions. In a past life I spent 20 years with coding Pascal being a significant bit of my job! Unfortunately for me, Vectorscript is feature poor and function rich so I spend most of my time either finding out what Vectorscript cannot do 'properly' or having to scan through the host of function calls available. Unfortunately the inadequacies of Vectorscript as a language means that to design reusable code properly is an uphill struggle and it will be, for me, simply be thought of as its name suggests, a scripting language which is more full of kludges rather than something that allows elegant modular reusable code that a 'proper' programming language facilitates.

Share this post


Link to post
In the end, for the special case of only 2 vertices, I simply drew a line between start and end then converted to polyline with the aid of the very useful LNewObj function making it a cinch. Oddly enough, it created a 2 vertex polygon!

Actually, it's not that odd. Polygons are really a degenerate case of Polylines. Remove all the curvy parts, show all the sides, and you've got a Polygon. Since a Line has no curvy parts, it defaults to becoming a Polygon when converted. You can create a Polyline with only Corner Points, but you have to be careful you don't cajole VW into "downgrading" it for you. The method I outlined above is the only way I know of to create a 2-vertex Polyline via VS. There may be others, but I usually stop looking after I find the first way that works.

I agree with you that VS is a function-rich language, but I don't think of the language as feature poor, but rather the compiler. VS is not a fully implemented version of Pascal, allocating memory for structures like Linked Lists is missing, and a few other features, but as a whole, it is still very powerful. Of course, there's always the SDK, but you have to get really dirty to develop there. For "quick and easy", it's VS. OK, that last line can be taken with a grain of salt, but there really are a lot of quick and easy uses for VS.

Reusable code is not that hard to generate, but it all hinges on the discipline and experience of the programmer. VW does not really facilitate the programming side of things and it is up to the individual to ferret out the details. That said, I have gotten excellent support when needed from the people at NV. Vlado, for one, has been invaluable and enlightening in really tough situations.

If you want to get more out of VW through "scripting", stay connected to like minded people.

Welcome to the mix,

Raymond

Edited by MullinRJ

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.

×