DesignCAD 3D Max 2027 box
Now available

DesignCAD 2027

Hatches that follow their boundaries, an on-screen dial to turn your model, and a way to find any command by name, in a clean Windows 11 look.

arc tangent to line

Started by bdeck, November 22, 2008, 03:46:14 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bdeck

Here's a useful macro to draw an arc centered on a point and tangent to an existing line.

Eliminates need to construct normals.

Extra point allows user to set arc to draw in either direction.


BD


Edit 11/22/08:   added (probably unnecessary) sys(1)=0 in cleanup 
                      added  selectonly constraint to extend command
Edit 12/08/08    corrected typo changing "z(1)-z(2)" to "z(1)=z(2)",  etc             
Edit 12/11/08:   added "z(3)=z(2)" and "z(1)=z(3)"  tests
Edit 12/12/08    added "sys(470)=1"

'arctan.d3m   arc tangent macro  11/08  bdeck
'Creates an arc centered on point 1 and tangent to an existing line selected with point 2.
'
'Set 4 points
'      1= arc center
'      2= select line for tangent
'      3= arc direction
'      4= end angle of arc
'
' Mainly 2d, but works in 3d if confined to a plane.
'
'
Alias CA_SPANANGLE  &H0305
Alias ENT_POINT  &H010E 

dim x(5), y(5), z(5)
ne=sys(9)
sys(1) = 0
sys41=sys(41)   
sys(41)=0
sys242=sys(242)
sys(470)=1

gosub archive    ' archive current selection, if any

>deselect
{
}

Setpoint "Set 4 points for  1) arc center, 2) tangent line, 3) direction, 4) end angle", 3

If Sys(1) < 3 Then goto cleanup

for j = 1 to 3                   
        pointval x(j), y(j), z(j), j             'store point values
next j

sys(36)=2

>arc4
{
<Type 0
<Radius -1
<Pointxyz [x(1), y(1), z(1)]
<Pointxyz [x(2), y(2), z(2)]
<Pointxyz [x(3), y(3), z(3)]
}

if sys(9)=ne then goto cleanup

entity sys(9)
query CA_SPANANGLE, spangle
>eraselast     
{
}

>PerpendicularTo
{
<SelectOnly 0
<Pointxyz [x(1), y(1), z(1)]
<Pointxyz [x(2), y(2), z(2)]
}

if sys(9)=ne then goto cleanup

entity sys(9)
query ent_point, 2 x(2) y(2) z(2)
>eraselast     
{
}

if x(1)=x(2) and y(1)=y(2) and z(1)=z(2) then goto cleanup
if x(3)=x(2) and y(3)=y(2) and z(3)=z(2) then goto cleanup
if x(3)=x(1) and y(3)=y(1) and z(3)=z(1) then goto cleanup

if spangle < 180 then                     'draw counterclockwise arc

sys(36)=0

>arc4
{
<Type 0
<Radius 0
<Pointxyz [x(1), y(1), z(1)]
<Pointxyz [x(2), y(2), z(2)]
<incomplete
}

else                                        ' draw clockwise arc

sys(242)=1
>arc
{
<Type 0
<Radius 0
<Pointxyz [x(1), y(1), z(1)]
<Pointxyz [x(2), y(2), z(2)]
<Angle .1
}

        if sys(9)=ne then goto cleanup
        entity sys(9)
        query ent_point, 3, x(3), y(3), z(3)

>sethandle
{
<type 0
<Pointxyz [x(1), y(1), z(1)]
<Pointxyz [x(3), y(3), z(3)]
}
       
>move                             ' rotate the arc backwards .1 degree
{
<type 1
<Reference 0
<Pointxyz [x(1), y(1), z(1)]
<Pointxyz [x(2), y(2), z(2)]
}

       sys(36)=0                                  ' turnoff silent mode
        entity sys(9)
        query ent_point, 2, x(2), y(2), z(2)

>Extend                                    ' complete the arc                           
{
<SelectOnly 1
<Pointxyz [x(2), y(2), z(2)]
<incomplete
}

if sys242=0 then sys(80)=0                   ' deselect

endif

cleanup:
       sys(1)=0
        sys(242)=sys242
        sys(41)=sys41
        if ns>0 then
gosub restore
gosub regenlite
endif
sys(3) = clay
end


'subroutines follow


archive:      '  archive current selection set
if sys(80)>0 then
    ns=0
    ne=sys(9)
    dim entsel(ne+1)
    for i=1 to ne
       getattr i,t,sel
       entsel(i) = sel mod 2  ' nearest thing to a bitwise (AND 1) available in DC
       ns=ns+entsel(i)
    next i

    sys190=sys(190)    ' archive handles
    sys191=sys(191)
    sys192=sys(192)
    sys193=sys(193)
    sys194=sys(194)
    sys195=sys(195)
    sys202=sys(202)
    sys203=sys(203)
    sys204=sys(204) 
  endif
return

restore:    ' restore original selection set and handles
    if ns>0 then
       For i = 1 To ne   'Reselect all prior selected items
          if entsel(i) then
               Getattr i, type, sel
               if  sel <> 1 then  Putattr i, type, 1    ' eliminate prior selection flag for all selected ents 
           endif
        Next i
    endif
   
    >SetHandle
    {
<Pointxyz [sys190, sys191, sys202]
<Pointxyz [sys192, sys193, sys203]
<Pointxyz [sys194, sys195, sys204]
<Type 0
    }

return

regenlite:           ' fast redraw of selected areas only
>move   
{
<type 1
<Reference 1
<Pointxyz 0 0 0
<Pointxyz 0,0,0
}
return


bdeck

#1
Here's an improved version that works with less housekeeping code.

BD

           
Edit 12/12/08    added "sys(470)=1"


'arctan.d3m  v. 2  Tangent Arc  Macro  12/08  bdeck
'Creates an arc centered on point 1 and tangent to an existing line selected with point 2.
'
'Set 4 points
'     1= arc center
'     2= point to designate existing tangent line
'     3= arc direction
'     4= end angle of arc
'
' Mainly 2d, but works in 3d if confined to a plane.
'
Alias CA_SPANANGLE  &H0305
Alias ENT_POINT  &H010E 

dim x(5), y(5), z(5)
ne=sys(9)        ' no of entities
sys(1) = 0        ' no of points set
sys41=sys(41)            ' archive rubberband type
sys242=sys(242)                  ' archive status of select-when-created flag
sys(470)=1

sys(41)=1        ' rubberband optional
Setpoint "Set 4 points for  1) arc center, 2) starting point, 3) direction, 4) end angle", 3
sys(41)=0        ' turn off rubberband

If Sys(1) < 3 Then goto cleanup2

for j = 1 to 3                   
  pointval x(j), y(j), z(j), j             'store point values
next j

if x(1)=x(2) and y(1)=y(2) and z(1)=z(2) then goto cleanup
if x(3)=x(2) and y(3)=y(2) and z(3)=z(2) then goto cleanup
if x(3)=x(1) and y(3)=y(1) and z(3)=z(1) then goto cleanup

sys(36)=1
sys(242)=0         'turn off select at creation

>arc4
{
<Type 0
<Radius -1
<Pointxyz [x(1), y(1), z(1)]
<Pointxyz [x(2), y(2), z(2)]
<Pointxyz [x(3), y(3), z(3)]
}

if sys(9)=ne then goto cleanup

entity sys(9)
query CA_SPANANGLE, spangle
>eraselast     
{
}

>Extend                      ' turn off select only flag                           
{
       <SelectOnly 0         'without this hack, "<SelectOnly 0" below will fail
       <Null
}

sys(242)=1         'turn off select at creation
>PerpendicularTo
{
<SelectOnly 0
<Pointxyz [x(1), y(1), z(1)]
<Pointxyz [x(2), y(2), z(2)]
}

if sys(9)=ne then goto cleanup

entity sys(9)
query ent_point, 2, x(2), y(2), z(2)

>selectprevious                                ' restore prior selection set, if any
{
}

>eraselast     
{
}

sys(242)=1         'turn on select at creation (deselects current selection set)

>arc
{

<Type 0
<Radius 0
<Pointxyz [x(1), y(1), z(1)]
<Pointxyz [x(2), y(2), z(2)]
<Angle .1
}

if sys(9)=ne then goto cleanup

if spangle < 180 then                'draw counterclockwise arc
         n=3
else                               ' draw clockwise arc
       n=2
endif

entity sys(9)
query ent_point, 3, x(3), y(3), z(3)
               
sys(36)=0                              ' turn off silent mode

>Extend                          ' now grab an end, and drag the desired arc                       
{
<SelectOnly 1         
<Pointxyz [x(n), y(n), z(n)]       
<incomplete
}

sys(1)=0
>selectprevious                                ' restore prior selection set, if any
{
}
if (sys(9)>ne) and (SYS242>0) then
       entity sys(9)
       change ent_selected 1
       update
endif
goto cleanup2

cleanup:
message "Failed"
cleanup2:

        sys(1)=0
        sys(242)=sys242
        sys(41)=sys41
end