Author Topic: ca2p.d3m (cylinder along two points in 3d space)  (Read 916 times)

prl

  • Hero Member
  • *****
  • Posts: 3389
  • A Bézier Extrusion
ca2p.d3m (cylinder along two points in 3d space)
« on: March 01, 2015, 10:42:34 PM »
You have two points in 3d space and you want to draw a cylinder between those points.

This version is user interactive, so feed it two points. 

prl=KWca2p.d3m

Also made a video showing it in action.  In the movie I'm gravity snapping to the points.

A movie - ca2p.d3m demo of creating a cylinder defined by 2 points in 3d space
(Reminder: In the flash movie, you can left click in the movie area to start/stop the action.  And the slider to rewind.)

Code: [Select]
'Program: ca2p.d3m (cylinder along two points)
 
'3/1/2015 forum topic http://forum.designcadcommunity.com/index.php?topic=6125.0

precision 15

curstate = sys(242)

'default data
rad = 1.5 'radius for cylinder, change this as desired
facet = 12 'facet count for cylinder, change this as desired

'if you prefer interactive questions for the radius and facet count, uncomment these two lines
'input "Enter cylinder radius", rad
'input "Enter number of facets for cylinder", facet

here:
setpoint "Point 1 for cylinder beginning, point 2 for cylinder end, press enter to END looping" 2
if sys(1) = 0 then goto Finish

sys(36)=2
pointval x1, y1, z1, 1
pointval x2, y2, z2, 2
sys(1)=0

'compute length in 3d spsace
dx = x2 - x1
dy = y2 - y1
dz = z2 - z1
d =  dx * dx + dy * dy + dz * dz

if d = 0 then
  message "error: 1st and 2nd points are identical, unable to determine unit direction vector."
  end
end if
 
rlength = sqrt(d)

'now draw a vertical cylinder at the ORIGIN, then matrix move it, the cylinder construction is easier at the origin
sys(242) = 1 'select object when created ON
>Cylinder
  {
  <Orientation 0
  <Nface [facet]
  <pointxyz [0, 0, 0]
  <pointxyz [rad, 0, 0]
  <pointxyz [0, rlength, 0]
  }

'set handles and move to proper location in 3d space
>sethandle
  {
  <type 0
  <Pointxyz [0, 0, 0]
  <pointxyz [0, rlength, 0]
  }
 
>move
  {
  <Reference 0
  <Type 1
  <Pointxyz [x1,y1,z1]
  <Pointxyz [x2,y2,z2]
  }
sys(80)=0   
sys(36)=0
regen
goto here

Finish:
sys(242) = curstate   
regen

End
« Last Edit: March 03, 2015, 12:48:02 AM by prl »

Bob P

  • Hero Member
  • *****
  • Posts: 1139
    • Era Replica Automobiles
Re: ca2p.d3m (cylinder along two points in 3d space)
« Reply #1 on: March 02, 2015, 06:18:08 AM »
I have two cylinder macros that work similarly, but require a diameter.  One also creates a centerline.

PRL:  Sorry, I should have given attribution, but I had forgotten where these came from.  I had written similar DOS macros myself, many, many years ago.

cyl2.d3m:
Code: [Select]
'Creates cylinder along line
'Program: cyl2.d3m (Cylinder along line)
precision 15

dx1$=sys$(0)
if dx1$ <> "" then goto skip

' Get User Input
dx1$=sys$(0)
if dx1$ = "" then input "Enter cylinder radius: ", dx1$

skip:

dx=val(dx1$)

input "Enter the number of cylinder facets: ", facet$
facet=val(facet$)

if sys(1) <> 2 then
sys(1)=0
setpoint "Point 1 for cylinder origin, point 2 for cylinder length " 2
endif
'sys(36)=1

pointval x1, y1, z1, 1
pointval x2, y2, z2, 2

sys(1)=0

>line
{
<pointxyz [x1, y1, z1]
<pointxyz [x2, y2, z2]
}

w = 2*dx
>PerpendicularPlane
{
<Width [w]
<pointxyz [x1, y1, z1]
<pointxyz [x1, y1, z1]
}

Entity Sys(9) ' Get the points for the square plane just drawn.
' only need the last two points

tp=sys(1)
tpm1=tp-1
Pointval x3, y3, z3, tpm1
Pointval x4, y4, z4, tp
sys(1)=0

'compute radius point location using two edges of plane
xr=(x4-x3)/2.0 + x3
yr=(y4-y3)/2.0 + y3
zr=(z4-z3)/2.0 + z3

>EraseLast 'remove the perpendicular plane
{
}


>Cylinder
{
<Orientation 0
<Nface [facet]
<pointxyz [x1, y1, z1]
<pointxyz [xr, yr, zr]
<pointxyz [x2, y2, z2]
}
End

and cyl3.d3m
Code: [Select]
'DesignCad 3D v14 Macro: cyl3.d3m
'
'This command takes two points specified for the cylinder's endcaps, 'and a specified radius to set 3 points: 1st point is one endcap,
'2nd point is for radius (and plane of endcap and orientation,)
'third specifies far endcap (and length.)
'
'This macro uses some basic linear algebra

PRECISION 8

DIM X(3), Y(3), Z(3)


user:

SetPoint "Set two points: one for each endcap" 2

If Sys(1)<2 then goto user

FOR I = 1 TO 2
PointVal X(I) Y(I) Z(I) I
NEXT I

'Reset points:

SYS(1)=0

INPUT "Now enter the cylinder's desired radius in drawing units: ",R



'First we must find an "R" orthogonal to P1 and P2 by using
'the vector cross product of P1 and P2: P1 x P2

RX = Y(1)*Z(2)-Y(2)*Z(1)
RY = X(2)*Z(1)-X(1)*Z(2)
RZ = X(1)*Z(2)-X(2)*Y(1)

MAGR = SQRT(RX*RX+RY*RY+RZ*RZ) 'Find the magnitude of "R"

'Now go find a unit vector "RHAT"

RHATX = RX/MAGR
RHATY = RY/MAGR
RHATZ = RZ/MAGR

'Now go find the location of third point relative to first point.
'This is found by multiplying the scalar (radius) R with RHAT:

X(3) = X(1) + R*RHATX
Y(3) = Y(1) + R*RHATY
Z(3) = Z(1) + R*RHATZ

'Now clear any set points:
SYS(1)=0

'Now set 1st point 1st for first end, 2nd point for radius,
'then last point for second end and then draw a cylinder

>Cylinder

{

<Pointxyz [X(1),Y(1),Z(1)]
<Pointxyz [X(3),Y(3),Z(3)]
<Pointxyz [X(2),Y(2),Z(2)]

}
END
« Last Edit: March 03, 2015, 02:05:56 AM by Bob P »