Jump to content
Developer Wiki and Function Reference Links ×

Handles for Dummies


Recommended Posts

I have a fundamental question about Handle variables. Please forgive the rudimentary level of the question and my grasp of VS:

I get an object - some geometry from the active layer

A:=FActLayer;

Let's say I want to duplicate that object. Call it B.

Will VS allow me to keep track of more than one object? Can I refer back to A after I do something w/ B?

What is the difference between Duplicate() and CreateDuplicateObject(?,?)?

I understand the Duplicate using an offset. I don't understand why CreateDuplicateObject needs two parameters, and what they are...

thanks

michaelk

Link to comment

As long as A & B exist , VW will track each. You can say that a handle (address) is the ID for each and usually it will be a LONGINT variable with 2M addresses available.

Duplicate is a procedure that works on selected objects which means that you will need to select the object to be duplicated before calling the procedure:

VAR

A,B: HANDLE;

SetSelect(A);

Duplicate(0,0);

B:= LNewObj;

CreateDuplicateObject is a function that takes a handle of the object to be duplicated and it can also place the duplicate inside a container such as a group, symbol or layer;

B:= CreateDuplicateObject(A,NIL);

If the container is NIL, it will place the duplicate in the same container as the original object.

Link to comment

Ahhhhhh.... That's what is meant by container....

Thank you.

I notice that as I test scripts, the value of A remains. Which makes sense.

Is there a command to remove values for a variable before running the script? (and is that the advisable way to handle it?)

michaelk

Link to comment

If you are assigning the handle at the beginning of each run of the script, there is no need to worry about what it was before the script ran.

It would be bad form (and provide really wacky results) to store a handle in a record and then try to reuse that. It would probably work for that time you ran VW, but if you restarted VW, or closed and reopened the file, the handles for all the objects are likely to change.

The four primary types of container objects are Layers (which are a special case), Groups, Symbols and Walls. You could consider Viewports and PIOs a type of container. And Crops and Annotations of Viewports are special instances of groups.

Keep asking as you get stuck and we can help you over the rough spots.

Link to comment
I notice that as I test scripts, the value of A remains. Which makes sense.

Is there a command to remove values for a variable before running the script?

Just to add to what's been said already:

The value of A exists in the file whether you assign it to a variable or not.

Variables do not retain their assigned values between runs of the script. They only exist while the script is running.

With each run the handle is re-assigned to the variable A, and as Pat has pointed out the actual value may change as conditions in the drawing change.

And since each time the script is run the handle to the object is retrieved afresh, we don't really care what the actual number value of the handle is. All we care about is that we are getting the handle to the correct object.

Link to comment

Thanks guys.

It's like playing with magic.

Is there a way - after the script has run - to select an object and ask it what it's variable was?

I'm watching the debugger as a script runs. I get values for all the handle variables, but an unexpected result at the very end. I'd like to click on each of the objects and inquire as to it's parentage.

michaelk

Link to comment

While the variables only last as long at the script, the handles to the objects should not change just because the script is done.

Put the following line in a blank script. You don't need a procedure declaration or run or begin/end unless you use variables, so you only need the one line. Or you could put this into your script at certain points for debugging purposes.

Message(FSActLayer);

This will show you the numeric representation of the handle of the first selected object.

The slightly more advanced version the I usually use is:

Message(Date(2,2),' Handle is: ',FSActLayer);

The Date(2,2) puts the time in first so you know that it is really from the most recent run and not leftover data from the night before.

Link to comment

Thanks, Pat.

Can you shed some light on creating geometry and handles?

I've been converting documents into VS just to see how simple geometry is built.

For example a circle from a document is something like: Arc(-1",1",1",-1",#360.0000000000d,#360.0000000000d);

(is that really the best way to draw a circle?!?!)

Searching the VSFR leads to Oval and OvalN commands to create circles. But in the OIP they are ovals...

But here's my question:

If I want to create a circle and assign it to a handle this doesn't seem to work

Cir:=Arc(-1",1",1",-1",#360.0000000000d,#360.0000000000d);

But this does:

Arc(-1",1",1",-1",#360.0000000000d,#360.0000000000d);

Cir:=FActLayer;

Is this just the way it works? Or is there a way to assign a new bit of geometry to a handle variable in one step?

thanks

michaelk

Link to comment

Ok, here is a quick primer:

There are two types of routines in VS. Procedures and Functions. When you look them up in the Function Reference, they tell you what type each is.

Procedures are just commands that do something, they do not have a return value. Functions return a value as their result, so they have to be assigned to a variable, or used in a chain that is expecting a result.

So, Arc is a procedure. It does not have a return value, so you can't directly assign it to a variable. (check out the ArcByCenter command it might be easier to make a circle).

GetType is a function that returns an integer. You have to assign it to a variable or use it as a parameter in another function.

Var N1:Integer;

N1:=GetType(FSActLayer);

or

Message(GetType(FSActLayer)

are both valid calls. You can use functions as parameters to other functions and procedures. Actually, you can next functions as deep as you can understand the logic.

So yes, that is just the way it works.

Also, please note that your using FActLayer. That returns the First Object on the active layer. If you have multiple objects on the layer, it will always return the same one. FSActLayer is the safer call. It returns the First Selected Object on the active layer.

Even better if you are drawing objects is to use the LNewObj which is a function that returns a handle to the last object created by the script. Note the LNewObj does not work when you are duplicating objects.

The call I would probably use is

ArcByCenter(0,0,5,0,360); {center x, center y, radius, start angle, end angle}

Cir:=LNewObj;

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