News:

Please remember to do a search before you ask a question!

Main Menu

Tracking an object in AL

Started by Andy H, March 24, 2021, 08:49:51 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy H

Haven't posted an animation for a while. So here is a simplified constraint animation, with a twist.  The original animation was going to be more complicated, But. Unfortunately the constraints kept breaking.  So I had to simplify things quite a bit.

This is just an example of using custom script, to track the movement of an object being animated, by its reference point.  And with a bit more coding, In theory one could code collision detection.

In order to track an object one ideally needs the objects ID (from the SDK).  There are a few ways to get it.  One way is to make the object an actor and use 'Alert Actors(0)(0)',  The first (0) is the actor index in the Actor listing  This is the order in which the actor was made, not the displayed order. The second (0) is for the objects ID number.

Another way is to write a simple macro.  Select a single object in the drawing and run the macro.
Dim gr  ' variable for graphic object
Dim TCSel  ' Variable for selected object
Set TCSel = ActiveDrawing.Selection ' Gets the selected object.
gr = TCSel(0).ID  ' Gets the ID number from first object in selection.
MsgBox("ID number = " & gr) '  Displays the ID number in a message box.

With this ID number, and a little custom script one can track the movement of the object.  In this case, the movement is used to draw a polyline using the AddNode command.  the ID number is for the circle at the end of the top end of the red line. 

The custom scripts is actually very simple, but it took me ages to figure out the correct syntax to use from the SDK documentation.

Dim refpt  ' Variable for ref point.
Dim TCdrw  ' variable for active drawing.
Dim gr ' Variabl for selected object.
Dim x ' Variable for x coordinate.
Dim y  ' Variable for y coordinate.

Set TCdrw = ActiveDrawing  ' Referencint the current active reawing.
Set gr = TCdrw.Graphics.GraphicFromID(9256)  ' Gets the object using the ID number.
Set refpt = gr.ReferencePoint(True) ' Gets the reference point position from the object.
' MsgBox("Ref point = " & refpt.x & ", " & refpt.y) '  Commented out - was just checking the ref point position.
x = refpt.x ' sets variable x to the ref point x coordinate, used to add a node to the polyline
y = refpt.y  ' sets variable y to the ref point y coordinate, used to add a node to the polyline

Set TCdrw = Nothing  ' Only needed after the animation is run. But I put this in wrong position.  It doesn't matter though it will still work.

One could play around with the line lengths and circle (pivot) positions, If one wanted to turn it into a proper Hoeken line example.   Regularly saving the file in case the constraints break.  Tcw file below, was done in v2020, but  save back to TC2017. The text and dimensions are on seperate layers for easy hiding.

Click Gif image below to start it animating


Tim Stewart

Hi Andy,

yes, a neat solution possibility, for similar mechanisms.

It's a shame the SDK is not better documented (like constraints).
That's not a hint!

It must have involved a lot of head scratching, and, patience.

Thanks for the post and insight. 👍
Regards Tim

You can design without engineering, but you cannot engineer without design.
Windows 10 Pro (19043.1165) 64-bit, TurboCAD Designer 2017-2020 and Pro. Plat. 2018-2020 + LW (64-bit) + AnimationLab 6
Intel i5 2500@3.3GHz 8GB Ram NVIDIA Quadro K2200

nikkipollard

Very cool.
Does the ID number of a graphic always stay the same?
Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

Andy H

Quote from: nikkipollard on March 26, 2021, 12:36:53 AM
Very cool.
Does the ID number of a graphic always stay the same?

As far as I know. When any object is created, it is given a unique ID number.  This number stays with the object and is saved in the file.  I tested by getting the ID in v2020, and saving back to 2017, and checking the ID number of the same object in 2017.  The object can be 2d, 3d, dimension or text.  They are all given a unique number.   

The numbers can also get a bit large.  I just created a dimension, and got an ID of 164233,  (its not that big of a drawing).  One thing one has to watch for is Booleans.  if a Boolean is carried out, the new object is given a new ID number.  the old ID stays with the old object as long as it is in the undo buffer (or part tree)

I did hit one problem.  For curiosity, I tried to get the ID on an old Windows 7 laptop.  But kept getting an error message when trying to run the macro.  So I don't know if my installation on the laptop is faulty, of if the macro just doesn't work on windows 7.

AussieBloke

Andy just wondering with using scripts  can you send a variable ( read ie cw for clockwise and ccw for ccw counter clockwise ) to a script so that an object can be rotated either way?

Say I had a script called direction and in that script I wanted to sometimes rotate cw and at other times to rotate ccw, so the call might be Direction(cw) and alternately Direction(ccw).

Does that make sense?

Andy H

Quote from: Darryl W on March 31, 2021, 02:17:39 PM
Andy just wondering with using scripts  can you send a variable ( read ie cw for clockwise and ccw for ccw counter clockwise ) to a script so that an object can be rotated either way?

Say I had a script called direction and in that script I wanted to sometimes rotate cw and at other times to rotate ccw, so the call might be Direction(cw) and alternately Direction(ccw).

Does that make sense?

Hi Darryl.

Assuming it can be done via code, and not 'on the fly'.  Then rotation direction can easily be changed by altering the rotate direction to either plus or minus sign.   

The actual code would depend on the desired outcome.  For example,  It could be a set number clockwise then anticlockwise.  Different numbers, like slow clockwise, and faster return (anticlockwise).  A random number (with maximum Limits set).  A user input using an InputBox (Not good as the animation would keep stopping requesting an input.  Reading a text attribute 'may' be possible, I've not tried it.

Something like a car windscreen wiper, should be pretty simple.  But something like Newtons cradle, much more fiddly (accounting to collision, gravity etc.).

Is there an example of the type of movement you are thinking of.
 


AussieBloke

In a few different animations I want to create they have parts moving around a polyline (  straight lines and arcs )  and these acis objects could easily be tested with circles for simple animation.

So by drawing a polyline and placing circles at equal distances around the polyline the call might be Direction( cw  ) at other times Direction ( ccw ).

Maybe a 2D rectangle with radiused corners would be enough to get started. What I dont know is how to call Direction from the al editor with either parameter/variable if thats possible.

The idea is to use your 1st post as the basis for moving an objects 2D  reference point via the x or y coordinate.

nikkipollard

#7
I don't know animation lab so this may be useless but

The way I test the direction of an arc in a polyline - using a function in a  script - there might be an easier method.
The poly arc has 4 vertices in this order (note the order is different for a normal arc - normal arcs are always anticlockwise)
start , centre. through , end
The Through point determines the direction of the arc.
If you measure the Angle between a line drawn from the centre vertex to the start vertex and  a line from the centre to the through point
The difference should be 90 degrees - negative or positive
The trick is to make sure that you measure the angles in a way that makes sure they are between 0 and 360 and in an anticlockwise direction - you also need to round the angles as a a small difference will give you the wrong result.
Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

Andy H

#8
Darryl.  It sounds like you will be using fixed object shapes. By that I mean polyline with Arcs, rectangle etc.  The method in my first post is best for when one does not know the outcome, I.e. I had not got a clue what shape the linkage would draw.  And by altering the linkage it would draw a different shape. It sounds from your description that the easiest method would be to use paths. 

Anyway.  Lets look at changing direction, which is the same for Move, or Rotate.   Assuming one knows when the change should occur.  This is just a simple example, there are a few ways to actually code the change.  The main to watch for with rotation is.  We know which way is clockwise, But for a TC object, it can be either way, and the default + degrees is actually anticlockwise.

The scrip below assumes + degrees is clockwise.

Dim d  ' [ Variable for direction ]
Dim fm  '  [ Variable for frame number ]
Dim pm  ' [ Variable for plus or minus direction.   We will use 1 for clockwise and -1 for anticlockwise ]

Sub StepCustomAction

fm = StepNum

If fm = 0 then  '  [ Sets the start initial parameters for the first frame.  a 'belt and braces' approach to ensuring no problem  ]
   pm = -1   '  [ Sets the initial plus/minus flag ]
   d = 6      ' [ Sets the initial start direction (eg. 6 units or 6 degrees) ]
End If

If fm / 17 = Int(fm / 17) then    '  [ Sets the change to happen every 17 frames ]
   If pm = -1 then      ' [ checks which direction is the existing one. ]
      d = 6      '   [ direction is change to clockwise (or left to right)]
      pm = 1   '  [ changes the plus / minus flag ]
   Else
      d = - 6    '  [ direction is change to anticlockwise (or right ot left)]
      pm = -1  '  [ changes the plus / minus flag ]
   End If
End If

End Sub

Then one would just use a command like
0, 169 (roughly 10 seconds at 17 frames per second), Move d, 0, 0
So for the first 17 frames (0 to 16) the object travels 6 units per frame (102 units). Then at frame 17 it moves minus 6 units per frame etc. 

For the movement in your rectangle / arc scenario, one just needs to be a little more creative, 

Andy H

Quote from: nikkipollard on April 01, 2021, 11:34:09 PM
I don't know animation lab so this may be useless but

The way I test the direction of an arc in a polyline - using a function in a  script - there might be an easier method.
The poly arc has 4 vertices in this order (note the order is different for a normal arc - normal arcs are always anticlockwise)
start , centre. through , end
The Through point determines the direction of the arc.
If you measure the Angle between a line drawn from the centre vertex to the start vertex and  a line from the centre to the through point
The difference should be 90 degrees - negative or positive
The trick is to make sure that you measure the angles in a way that makes sure they are between 0 and 360 and in an anticlockwise direction - you also need to round the angles as a a small difference will give you the wrong result.

A neat solution Nikki. And not something I would have thought of.

nikkipollard

It seemed easier then all the solutions I got when I tried googling it
Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

AussieBloke

Thanks Andy I will test it out shortly.

AussieBloke

#12
Heres a simple animation of rotating cylinders around an object shaped like a track, this animation could serve as the basis of animating sprockets and chains, tracked object. There is 1 error that I discoverd after creating the gif file where 1 of the cylinders jumps, I suspect that its a workplane problem where all other objects are fine. Anyway I chose not to bother about it since this is just a sample of whats possible.

Andy it would be great if this simple demo could serve as the basis for changing direction by a script for both directions, its the direction call in the script that I need to learn more about.

Note I originally posted the gif sample earlier today but noticed it didnt work from the forum, if it doesnt work for you then download the source file its a V2020 version.

Andy H

Hi Darryl.   I replaced your gif image with the animation.

To convert to a reverse direction script will take some thinking about.  As obviously it could not be done in conjunction with key-frames (which records fixed reference point positions),  Unless of course one just records a second set of key-frames in the opposite direction. 

So for a script, the positions / rotation would have t be changed to commands. Which is not difficult, but would require the groups altering.   I'll have a think.

AussieBloke

Andy thanks for changing the gif file, for some reason mine was corrupted but it worked okay on my pc.

As I mentioned this was just an example using keyframes, plugging in the reverse actions is again simple.

I was thinking about using your previous suedo code as a starting point but its calling the script direction thats got me stuck. So how can I call Direction(-1)  or Direction(1) as a starting point or am I missing something else?


Andy H

Quote from: Darryl W on April 05, 2021, 10:22:24 AM
I was thinking about using your previous suedo code as a starting point but its calling the script direction thats got me stuck. So how can I call Direction(-1)  or Direction(1) as a starting point or am I missing something else?

Setting the initial direction would normally be done in the first frame (frame 0).  In the above example, I simply used, If fm = 0. the added whatever is required in that, If / then / End If block.   Because the scrip above is set to change direction. the initial start (-1) is opposite to what is required.   The 1 and -1, can actually be anything.  I just chose them as its easy to visualise, 0  standing still, 1 for positive direction. -1 for negative direction. 

If one was writing a more complicated script, the it would probably be an advantage to put the direction and units bit into a function.  Then call the function when required.