Jump to content
Developer Wiki and Function Reference Links ×

AddSurface - Adding more than two objects together


twk

Recommended Posts

{..script for JTopC Creation..}

JkTopC := LNewObj;

{..script for JkBotC Creation..}

JkBotC := LNewObj;

{..script for JkEndC Creation..}

JkEndC := LNewObj;

{JkTrAdd is a dummy handle to add surfaces}

JkTrAdd := AddSurface(JkEndC, JkBotC, JkTopC); {Add Chords}

I understand the AddSurface function only allows two handles to be added.. and my above scripting is incorrect.

Is there anyway to add more than two objects in one function.. because I'm thinking that the only other way is to create another handle JkTrAdd2 and then it would be

JkTrAdd2 := AddSurface(LNewObj, JkTopC);

or how would I go about this?

Link to comment
My script is actually a snippet of a PIO.. would this matter?

Yes. You will have to add them together 2 at a time. Are you always adding the same number of shapes? Or a known set number of shapes? If so, you can write a function that takes all of their handles and will combine them. If the number of shapes has an unknown upper limit, then I'd suggest storing your handles in an array (or DynArray) and process them from the array.

Raymond

Link to comment

Ahh write a function.. I think thats what I want to do.. I've seen that alot in the Vectorscript Manual, but I just dont know how to implement it.

I should say I 'just started' getting my head around scripting.. :)

Back to the script.. there is a known number of objects to combine.. Just 4 polys.. How would I write the function script you mentioned?

Thanks

Tui

Link to comment

Tui,

For new coders the concept of handles can take some getting used to, so maybe this will help:

A handle is a temporary unique identifier applied to every object handled by the script. I think you may be confusing handle variables, which are just pointers to an object, with handle data, the unique tag attached to each object.

In your case, AddSurface gives you a handle to the newly created surface. As the original objects no longer exist, the argument handle variables no longer point to objects.

Assume JkEndC, JkBotC, and JkTopC are all handle variables pointing to valid 2D objects.

JkTrAdd:=AddSurface(kEndC, JkBotC);

JkTrAdd:=AddSurface(JkTrAdd, JkTopC);

JkTrAdd now points to the added surface of all three objects. If you want your code to be more foolproof, you could check that any of the handles <>NIL before each add step.

HTH,

Josh

Link to comment

Tui,

???A function is a user defined set of VS commands that can be called in your main program as a single statement. Functions can also be nested, so they can be very complicated, but if they are well written, they will make you code easier to follow. The reason you write functions is to simplify your program by putting commands together that perform a higher level operation, and at the same time make your program more readable.

???Without a function, your idea to add 4 polys together might look like this (with handles JkTopC, JkBotC, JkLeftC, and JkRightC that point to four different polygons):

AddPolyHnd := AddSurface(JkTopC, JkLeftC);

AddPolyHnd := AddSurface(AddPolyHnd, JkBotC);

AddPolyHnd := AddSurface(AddPolyHnd, JkRightC);

???It is assumed that all the polys overlap to some degree, so the order of addition is not important.

???A function to do the same thing might look like:

FUNCTION Add4Polys(PolyH1, PolyH2, PolyH3, PolyH4 :Handle) :Handle;

VAR

???RsltPolyH :Handle;

BEGIN

???RsltPolyH := AddSurface(PolyH1, PolyH2);

???RsltPolyH := AddSurface(RsltPolyH, PolyH3);

???RsltPolyH := AddSurface(RsltPolyH, PolyH4);

???Add4Polys := RsltPolyH;??????{ This is the return value }

END;??????{ Add4Polys }

???To use the function in your code you would create all your poly objects and assign them to handle variables like you did before, and then call your function with:

...

???AddPolyHnd := Add4Polys(JkTopC, JkBotC, JkLeftC, JkRightC);

...

???This is very basic and does not cover all possible combinations of events, like two polys do not touch so they do not add, or one poly did not get created so its handle is NIL before you start. A well written function will contend with these possibilities so the code won't bomb at runtime if something goes wrong. It may even post a message alerting the user that something unexpected happened.

???There are different forms of user defined FUNCTIONS. If a function does not generate a return value it is a PROCEDURE. Both procedures and functions can have arguments that are variable, meaning the variables that are passed to the function can be changed by the function and will stayed changed when the function finishes.

???See any book on PASCAL Programming for more and better examples. Also read the VectorScript User Guide which explains differences in VectorScript from standard PASCAL.

HTH,

Raymond

Link to comment

Hi Guys Thanks for the help. Really appreciate it.

Josh you were saying that JkTrAdd is a handle(pointing to the newly added surface) and this is temporary? So trying to call a function or calculation further down the line of code using JkTrAdd would not be possible? Like for instance I wouldn't be able to say HDuplicate(JkTrAdd,x,y)?

Nevertheless I am slowly understanding the syntax usage.(keyword slowly l:) )..

And Raymond thanks for the tips on 'Function' and its functions..lol.. but seriously, after you had suggested to use a function I went digging through the VS manual, and there it was, together with user-defined procedures.. It seems they can be very useful. However it also looks like a bit of a mission to implement. As the examples(in the manual)suggest, they are created so they can be 'easily' retrieved later. And also as you had mentioned, it simplifies the code. From your example in of itself I can see more lines of code then Adding the surfaces one by one as your example showed(the example just before the function one). I gather that the function serves a purpose where somewhere down the line I wanted to added four other polygons I could just call that particular one (Add4Polys) and feed in the new polys? Is this correct?

For what its worth, the two replies in this post alone have expanded my knowledge immensely and I really appreciate such help. So thanks again.

Also on a side note.. I noticed not a lot of people post their codes up here for others to see. Is there a legal reason for this? (Like a rule for the forum?). Would be nice to have a place where we could share our codes and maybe get others to comment on it, like you guys have done, to make it better, or improve it. Does such a place exist for vectorscripting?

Thanks again. Tui

Link to comment
As the examples(in the manual)suggest, they are created so they can be 'easily' retrieved later. And also as you had mentioned, it simplifies the code. From your example in of itself I can see more lines of code then Adding the surfaces one by one as your example showed(the example just before the function one). I gather that the function serves a purpose where somewhere down the line I wanted to added four other polygons I could just call that particular one (Add4Polys) and feed in the new polys? Is this correct?

Yes, the benefit comes when you want to call the code again. If you were only adding four polys once, then you wouldn't need make a function. But if you were going to use this approach in another program you had planned, then there might be a benefit to making a function. It's a judgement call.

Typically, I write everything inline, then when I see a pattern in the code that looks like a complete thought I make a function/procedure of it. It is a learned habit. If you write enough code, you will have an idea when it will serve you best. It usually does make your code easier to read.

I noticed not a lot of people post their codes up here for others to see. Is there a legal reason for this? (Like a rule for the forum?

No legal reasons, except maybe ownership. This forum has a place near the bottom of the front page "Vectorworks Resource Share - VECTORSCRIPT" where people do share examples. This section is more for problems, but examples are welcome and you will always get helpful comments to requests for improvement.

All the best,

Raymond

Link to comment

Once you set a handle variable to point to an object, you can do any handle operation to that variable and it will affect that object; HMove, HDuplicate, AddSurface, etc.. As long as the operation doesn't destroy the object. For example, once you apply add surface to two objects, the original objects no longer exist.

You can also set the variable to point to a different object. Say, for example, you have a script that creates and modifies text three times, you can call txtHan:=LNewObj three times, and txtHan will end up pointing to three different objects.

Handles are temporary in that their values do not persist from script to script. You can't, for example, store a handle to a database and then find the same object by reading that value on a subsequent script run.

-Josh

Link to comment
Handles are temporary in that their values do not persist from script to script. You can't, for example, store a handle to a database and then find the same object by reading that value on a subsequent script run.

Tui,

??If I may add to what Josh said: ?Josh is mostly right, but there's a gray area. The value of a handle does not change at random, or after a script is run. It exists as long as the VW file is open.

???A handle to an object holds an unique number. You can see the number using the MESSAGE() command, if you like. You can't modify a handle's value, but you can view or test it to see if it changes.

???Try a script with the line: Message(FSActLayer);

???Move the object, stretch it, change its color, send it forward or back, send it to another layer (w/ the OIP). Each time you do something rerun the script. Now CUT the object and PASTE it back again. Rerun the script. It now has a new handle.

???As long as your file remains open, the value of a handle does not change unless you (or VW) modify the object in such a way that results in a change. Some operations will change handle values, like Add Surface, and Cut/Paste; while others will not, like Move, Scale, and Rotate.

???Over the long haul, handles are temporary. When you close then reopen a file, VW assigns new handle values to every object, so you cannot use stored values of handles for long term reference. Over the short haul, it works.

???In summary: You can store handle values between running a script once and then running it again or running another script, or editing the file. If the objects are still there, their handles are, too. When you close the file, the handle values will be different when the file is reopened.

Raymond

Link to comment
  • 2 weeks later...

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