News:

Please note new Forum URL: forum.designcad.com

Main Menu

How can I move solids from within a macro?

Started by Dempsey, August 16, 2008, 07:32:18 PM

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Dempsey

The ultimate goal is to select a number of objects (typically solids) and then run this macro. I like to determine the center of each selected solid, set a handle at its center and then use that handle to move each solid in a certain direction. Right now I can only select one solid and move it.


' test.d3m
  INCLUDE "DcadAlias.d3i"
'
' NOTE: Right now suitable for one selected solid only
'
  Found = 0
  FOR i = 1 TO SYS(80)
    Getselect i, Ent
    Getattr Ent, Type, Select, Laynum, Group, Red, Green, Blue, Solid
    ENTITY Ent
    IF Found = 0 THEN
      QUERY Ent_MinBound minX, minY, minZ
      QUERY Ent_MaxBound maxX, maxY, maxZ
      Found = 1
    ELSE
      QUERY Ent_MinBound, newX, newY, newZ
      IF newX < minX THEN minX = newX
      IF newY < minY THEN minY = newY
      IF newZ < minZ THEN minZ = newZ
      QUERY Ent_MaxBound, newX, newY, newZ
      IF newX > maxX THEN maxX = newX
      IF newY > maxY THEN maxY = newY
      IF newZ > maxZ THEN maxZ = newZ
    END IF
  NEXT i
  '
  ' ----- calculate centre of object
  '
    X = (minX + maxX) / 2
    Y = (minY + maxY) / 2
    Z = (minZ + maxZ) / 2
    MESSAGE "center x,y,z=", X, Y, Z
    >SetHandle
    {
      <type 1
      <pointxyz X, Y, Z
      <pointxyz X, Y, Z
      <pointxyz X, Y, Z
    }

    X = X + 2       ' modify later and add a certain percentage instead
    Y = Y + 3
    Z = Z + 5
    >Move
    {
      <Type 1
      <Reference 0
      <Pointxyz X, Y, Z
      <pointxyz X, Y, Z
      <pointxyz X, Y, Z
    }
''    >DragMove
''      {
''        <pointxyz X, Y, Z
''      }
  REGEN
  END


I ran into several problems and appreciate some help.

  • Each solid has a grid header and from zero to several planes. Do I do the right thing the way I determine the min and max X, Y and Z of the bounding box of a solid?
  • Do I set the handle correctly?
  • How come that the move (or dragmove) command always position the object such that the center handle ends up in the origin (0,0,0)?
Dempsey

bdeck

#1
Hi Dempsey,

As DT always says,

"Dont forget those square brackets!"   

It's nice to know that I'm not the only one who has that problem.  ;D


BD

PS: If you are not rotating the selection, you need only one handle.



' test1.d3m
  INCLUDE "DcadAlias.d3i"
'
  Found = 0
  FOR i = 1 TO SYS(80)
    Getselect i, Ent
    Getattr Ent, Type, Select, Laynum, Group, Red, Green, Blue, Solid
    ENTITY Ent
    IF Found = 0 THEN
      QUERY Ent_MinBound minX, minY, minZ
      QUERY Ent_MaxBound maxX, maxY, maxZ
      Found = 1
    ELSE
      QUERY Ent_MinBound, newX, newY, newZ
      IF newX < minX THEN minX = newX
      IF newY < minY THEN minY = newY
      IF newZ < minZ THEN minZ = newZ
      QUERY Ent_MaxBound, newX, newY, newZ
      IF newX > maxX THEN maxX = newX
      IF newY > maxY THEN maxY = newY
      IF newZ > maxZ THEN maxZ = newZ
    END IF
  NEXT i
  '
  ' ----- calculate centre of object
  '
    X = (minX + maxX) / 2
    Y = (minY + maxY) / 2
    Z = (minZ + maxZ) / 2

    MESSAGE "center x,y,z=", X, Y, Z

    >SetHandle
    {
            <Type 0
            <pointxyz [X, Y, Z]
     
    }

    X = X + 2       ' modify later and add a certain percentage instead
    Y = Y + 3
    Z = Z + 5
    >Move
    {
      <Type 1
      <Reference 0
      <Pointxyz [X, Y, Z]
     
    }
'    >DragMove           
'      {
'      }
 
  END


Or Better Yet

' test2.d3m

     dX =  2       ' modify later and add a certain percentage instead
     dY =  3
     dZ =  5

    sys(36)=1   ' silent mode

    >Move
    {
      <Type 1
      <Reference 1              ' Handles? We don't need no stinking handles! (Treasure of the Sierra Madre)
      <Pointxyz 0,0,0
      <Pointxyz [dX,dY, dZ]
     
    }
 
  END

Dempsey

Thanks bdeck, that solved two of my problems, and thanks for the silent mode tip.

That still leaves the question how I can determine the min and max X, Y and Z of the bounding box of a solid.
Dempsey

DrollTroll

Sys() functions to the rescue!

196 - Selection Minimum X [-10e6<=x<=10e6]
197 - Selection Minimum Y [-10e6<=x<=10e6]
198 - Selection Maximum X [-10e6<=x<=10e6]
199 - Selection Maximum Y [-10e6<=x<=10e6]
200 - Selection Minimum Z [-10e6<=x<=10e6]
201 - Selection Maximum Z [-10e6<=x<=10e6]

Just select the solid (or any entity or entities of interest) and check these sys functions.

25 years with DesignCAD

bdeck

#4
Quote from: Dempsey on August 17, 2008, 12:24:48 AM
That still leaves the question how I can determine the min and max X, Y and Z of the bounding box of a solid.

Hi Dempsey,

For individual entities, your query statements appear to work correctly.

Here are some sys values that might be simpler for finding min and max of the entire selection.

196 - Selection Minimum X [-10e6<=x<=10e6]
...
201 - Selection Maximum Z [-10e6<=x<=10e6]         Oops, looks like DT already posted.


Also, Regen appears to be superfluous when used after a move statement.

I sometimes even use zero-distance moves on partial view areas to avoid having to do a regen on the entire view.

BD


Heres yet another way:

' test3.d3m

    sys(36)=1

    >SetHandle
    {
            <Type 1  ' set handle in center
                 
    }

    x=sys(190)   'Selection Handle 1 X value [-10e6<=x<=10e6]
    y=sys(191)   'Selection Handle 1 Y value [-10e6<=x<=10e6]
    z=sys(202)   'Selection Handle 1 Z value [-10e6<=x<=10e6]


    dX = 2       ' modify later and add a certain percentage instead
    dY = 3
    dZ = 5


    >Move
    {
      <Type 1
      <Reference 0
      <Pointxyz [X+dx, Y+dy, Z+dz]
     
    }


  END


bdeck

QuoteFor individual entities, your query statements appear to work correctly.


OK, I see the problem now.

I had been trying your code on simple box solids, where your code works correctly.

On other types of predefined solids, like spheres, the bound query fails, or changes depending on the location of the solid.

Looks like a bug report for DT.

BD

Dempsey

#6
DT, the issue is that I like to select say 5 solids at the same time from a drawing with say 10 solids, and then run the macro. Each of the selected solids I like to move independently to a different location according to some formula (which is not relevant for this discussion). The SYS(196) to SYS(201) I cannot use as that gives the min and max boundaries of the combined selected 5 solids. I was hoping to use the QUERY Ent_Min/MaxBound, but as bdeck also observed those sometimes return incorrect values for solids.

What I have done so far is this:

  • I save the list of selected entities.
  • Then for each entity I take its first point and do a PointSelect to get only that entity selected.
  • Now I can use either SYS[196] to SYS(201) to determine the center and set a handle there, or I can use bdeck's suggestion to set a handle at the center (type 1) and use SYS(190), SYS(191) and SYS(202) to get the handle (and center) position.
  • After that I can Move or DragMove the entity using the handle.

The problems I still need to solve are:

  • When two entities have the same first point, PointSelect may not select the entity I want to select. I believe I have a solution for that.
  • I need to include special code for processing a group or a solid only once although each can have multiple entities.
  • At the end of the macro only the last moved entity is selected, so I will have to reselect the entities to make it look nice.
  • By setting handles I basically "destroy" original handle(s). I don't know if that is good or bad.
  • What I think is really bad is that after the macro execution when I like to do and undo, I have to do multiple undo's to get back to the begin situation before the macro execution. I vaguely recall that that issue was discussed before, but I have not found the forum topic yet.

Am I missing the obvious simple solution here? I am not new to programming, but I am new to the BasicCAD interface to a DesignCAD 3D Max drawing.

What I try to accomplish with one execution of the macro is illustrated in the attached two PDF files, one before and one after. Ignore the dimensions and text.
Dempsey

DrollTroll

Quote
  • When two entities have the same first point, PointSelect may not select the entity I want to select. I believe I have a solution for that.

You can use the macro command
>ID_SELECT_NEXT
{
}


-- it's what happens when you press the Enter key with an object selected. I thought we had a "friendly name" for it, but it seems not. Even with this command, however, verifying the correct item through code can be a bear.

Quote
  • What I think is really bad is that after the macro execution when I like to do and undo, I have to do multiple undo's to get back to the begin situation before the macro execution. I vaguely recall that that issue was discussed before, but I have not found the forum topic yet.

There's no "undo to previoius marker" capability in DesignCAD. The best I can recommend is have the macro save a temp file, run the code, ask if you like the results, and if not re-load the temp file.


QuoteAm I missing the obvious simple solution here?
Not that I can see -- this is really a rather difficult operation unless you have a much more object-oriented drawing database.

Maybe you can save logical groups of items that will be "exploded view" processed at once to separate layers? Then when you do your PointSelect, verify the correct layer for the selected object...

25 years with DesignCAD

Dempsey

DT and bdeck,

Thanks for your help and feedback. Using your suggestions I will continue on my current approach and publish the macro if and when successful.
Dempsey

bdeck

#9
Quote from: Dempsey on August 18, 2008, 07:49:37 PM
Am I missing the obvious simple solution here? I am not new to programming, but I am new to the BasicCAD interface to a DesignCAD 3D Max drawing.


Hi D,

From your discussion it appears that "handles" and "bounds" are two separate issues.

1) If you are not dragging or rotating the solids, I would not use handles at all.  The sample code test2.d3m above moves the enties without handles. (Dragging by a centerpoint in 3d can lead to really messy results.)

2) To find bounds individually, you could use GETSELECT to record the entity number of each selected entity, then Deselect{} to clear the selection set, then Putattr or Change to reselect them individually and find their bounds using sys(196)-sys(201), and then use Puttattr or Change to select them all again.

Unfortunately, the sys values fail also when used in this way on some solids unless you first explode them.

EDIT: Also, after reselecting either with putattr or change, the move command has strange effects on some solids. (ie cone) We really need a select-by-entno command that works.

Nothing is simple or obvious.

BD


' bounds.d3m
' finds bounds of defined solids within a selection.
' Ignores solids included in blocks. Groups are ok.

n=sys(80)
dim ent(1+n)

for i=1 to n
    getselect i,ent(i)
next i

>deselect
{
}

for i=1 to n

   getattr ent(i),t
   if t=32 then
      putattr ent(i),t,1
      >explode               ' includes it own regen to show the selection
      {
      }

      print t, sys(196),sys(197),sys(198),sys(199),sys(200),sys(201), "   Hit a key"
      anykey a
      >undo    'reassemble the exploded solid
      {
      }
      >deselect
      { 
      }

   endif
next i

for i=1 to n  'reselect the entire batch

   getattr ent(i),t
   putattr ent(i),t,1
 
next i

regen  'display highlighted solids

end


   

Dempsey

Thanks for the help bdeck.

Imagine that the center of each selected entity is connected to some central point with a virtual line. What I need to accomplish is to move each selected entity further away from that central point along the virtual line of that entity. That is how the center handle came into being.

I had already looked at test2.d3m and like the concept of not using handles. I figure when I use the center of an entity as the reference point that that satisfies the above criteria.

Your bounds.d3m is certainly much simpler and surer than my approach using PointSelect.

Let me digest all this, put it together and toy around with it. It might take a few days because of other activities I am involved with.

Dempsey

Dempsey

#11
I have been playing around with solids without using the Explode command for the simple reason I don't like a workaround that does not always work.

When a sphere is selected it returns in the selected list only one type 32 (grid header) entity. When I select that solid using the "PutAttr Ent, Type, 1" the SYS(196) to SYS(201) returns the wrong results. However, as I select also the related type 33 (grid line) entities I see the min/max values changing, and the min/max values are correct as soon as I have selected all the grid lines of the sphere.

The min/max of a box is correct when I select the grid header and the two planes. In this case selecting the two grid lines does not have any impact, most likely because the grid lines are within the planes.

It is therefore safe to say that all entities of a solid (types 31, 32 and 33) need to be selected in order to properly select a solid from within a macro.

This leave me with several interesting observations:

  • Why are type 33 entities not part of the selected entities list for a solid? Perhaps DT can answer that.
  • I have to figure out how to get the type 33 entities of a solid based on info in the grid header and/or plane entities.
  • What I really like to have is a general function (or macro) that can select a solid properly based on the grid header. Come to think about it I might write that macro myself.

Does this all make sense?

UPDATE: Found several forum topics from DT with the grid header/lines relationship. The grid header (type 32) entity gives in SYS(99) the number of grid line entities. The grid line entities (type 33) immediately follow the grid header entity. In case of a group (type 34) entity SYS(99) gives the number of entities in the group.
Dempsey

prl

#12
Dempsey, stuck my nose in briefly and found your interesting topic.  Maybe some of the below will help.


'Program SolidSelect.d3m

'Basiccad solid select test code for Dempsey

'Notes
'      This is for DesignCAD version 15.3 only - the exectuable helper OLE SelectSolidById15.exe
'      needs to be recompiled for each version

'      you need to put the SelectSolidByID15.exe executable in the executable directory, see
'      file paths under >options>options> File locations
'

Dim ent(2000), SolidIdList(2000)

if sys(80)=0 then
  message "Nothing selected. . . press any key to abort"
  END
end if

scount = 0 'solid counter
maxsel = sys(80)

'copy the selection set to the clipboard for undo routine
>sethandle
{
<type 0
<pointxyz [0,0,0]
}

>copy
{
}

'get entity numbers of selection and store in array for undo routine
for j = 1 to maxsel
  getselect j, ent(j)
next j
sys(1)=0
sys(80)=0


'create a list (array) of the solid ids - this is the key to selecting solids
for j = 1 to maxsel
  existflag = 0
  getattr ent(j), type, select, laynum, groupid, red, green, blue, solidid

  if solidid > 0 then
     for jj = 1 to scount
       if solidid = SolidIdList(jj) then
         existflag = 1
         exit for
       endif     
     next jj
     
     if existflag = 0 then
        scount = scount + 1
        SolidIdList(scount) = solidid
     endif
  endif
next j

sys(80) = 0
sys(1) = 0

Message "solids count = ", scount


'now using basiccad, select each solid individually and do something with selection
precision 0
for j = 1 to scount
  sys(80) = 0
  sys(1) = 0
  sys$(0) = SolidIdList(j) 'pass solidid to command line parameter for executable
  run "SelectSolidById15.exe", 2
  gosub DempseyCode
next j

sys(80) = 0
sys(1) = 0
regen

'undo routine
sys(1027) = 4 'yes/no message dialog
Message "Keep current?"
if sys(1028) = 7 then 'No - do the undo routine
  for j = 1 to maxsel
    getattr ent(j), type, select
    putattr ent(j), type, 1
  next j
 
  if sys(80)>0 then
  >erase
    {
    }
  endif
 
  >paste
  {
  <Type 1
  <SelectOnly 1
  <pointxyz [0,0,0]
  }
endif


'exit - add reset/restore system items here
sys(1)=0 
regen
sys(1027) = 0
END 'of main ***************************************************************************************


'**** subroutines follow
DempseyCode: 'some meaningless test code for now
  dx = dx + 5
  dy = 0
  dz = 0
 
  ' Get Coordinates of the Selection Handle
  OrgX = Sys(190)
  OrgY = Sys(191)
  OrgZ = Sys(202)
 
  >DragMove
  {
     <pointxyz [OrgX + dX], [OrgY + dY], [OrgZ + dZ]
  }
return



If it turns out the OLE helper program is useful to you, when I get back to my normal desk, I'll recompile it for versions 17 and 18.  That will not be until mid September though.

Cheers



Dempsey

Thanks prl, I will keep your offer in mind. Right now I believe I can make it work using pure macro only, which has the advantage that it can be used "as is" by other DesignCAD versions. Secondly, I use this project also as a learning experience to get a better understanding of the interaction between Macro and DesignCAD.
Dempsey

prl

Dempsey,  I think I wrote that OLE helper in 2004 because I was having trouble with the getattr and putattr not properly selecting all the pieces of a solid and I didn't like >pointselect. Can't remember if I ever checked this problem again or whether this can be solved with Query/Change now.

Have fun with it and remember basiccad is addictive!  Oh yes, too late for you, your hooked now. :)