Planes

Autocad has a point, a line, but no plane object. The Region object works fine.

AddRegion takes an array of autocad entities which must be a closed figure. This would make a triangle.

Sub plane_3pt(pt1() As Double, pt2() As Double, pt3() As Double)
 Dim lines(0 To 2) As AcadEntity

 Set lines(0) = line1(pt1, pt2)
 Set lines(1) = line1(pt2, pt3)
 Set lines(2) = line1(pt3, pt1)

 Dim regionobj As Variant
 regionobj = acadDoc.ModelSpace.AddRegion(lines)
 regionobj(0).EntityTransparency = 90

End Sub

in this sub to make a region from 4 pts i added an optional layer argument and a global variable to control transparency. To view a region you have to change the autocad visual style to anything except 2D Wireframe.

Sub plane_4pt(pt1() As Double, pt2() As Double, pt3() As Double, pt4() As Double, Optional strlayer As Variant)
 Dim lines(0 To 3) As AcadEntity

 Set lines(0) = line1(pt1, pt2)
 Set lines(1) = line1(pt2, pt3)
 Set lines(2) = line1(pt3, pt4)
 Set lines(3) = line1(pt4, pt1)

 Dim regionobj As Variant
 regionobj = acadDoc.ModelSpace.AddRegion(lines)
 regionobj(0).EntityTransparency = g_transparency

    If Not IsMissing(strlayer) Then
       regionobj(0).Layer = strlayer
    End If

End Sub

you can pass a polyline. it has to be a 3D polyline. if you do that, the array only needs one slot. You cannot pass the polyline directly to AddRegion

Sub plane_pl(pline As Acad3DPolyline, Optional strlayer As Variant)

Dim lines(0) As AcadEntity
Set lines(0) = pline

Dim regionobj As Variant
regionobj = acadDoc.ModelSpace.AddRegion(lines)
regionobj(0).EntityTransparency = g_transparency

    If Not IsMissing(strlayer) Then
       regionobj(0).Layer = strlayer
    End If

End Sub

a simple equation such as x=3 is a line in 2D space, but its a plane in 3D space.

this is the test code to create planes setting x = 1,2,3,4,5,6,7,8,9 and setting a layer with the same name and color.

init sets up the min and max for y and Z . I shouldnt run a loop counter with a double, but it didnt cause any trouble. my pt sub that creates point arrays expects a double.

Sub test_plane5()
  init
  Dim x As Double

    For x = 1 To 9
        pt1 = pt(x, ymin, zmin)
        pt2 = pt(x, ymax, zmin)
        pt3 = pt(x, ymax, zmax)
        pt4 = pt(x, ymin, zmax)
        
        plane_4pt pt1, pt2, pt3, pt4, x
    Next x
  Update
End Sub

to make 3D polylines to use in region making, i use an old sub i use for drawing 2D profiles, modified for 3D. its a little more complicated looking, but the purpose of it is to be able to make the point array with the VBA ARRAY statement. unfortunately autocad VBA Add3DPoly does not accept this array so it has to be copied to an array of doubles.

pts = Array(x, y1, z1, x, y1, z2, x, y2, z2, x, y2, z1)

this can be any number of points, the receiving sub counts the index numbers and sets up a for loop to copy.

here are 3 subs to draw planes parallel to the coordinate planes using this method.

Sub test_plane8()
  init
  
  xplane 2, "1"
  yplane 1, "2"
  zplane -2, "3"

  Update
End Sub


Sub xplane(x As Double, strlayer As String)
  Dim y1 As Double, y2 As Double
  Dim z1 As Double, z2 As Double
  Dim pts As Variant
    
  y1 = ymin: y2 = ymax
  z1 = zmin: z2 = zmax

      pts = Array(x, y1, z1, x, y1, z2, x, y2, z2, x, y2, z1)
      Call draw_3D_array(pts)
      plane_pl g_3D_pline, strlayer
End Sub

Sub yplane(y As Double, strlayer As String)
  Dim x1 As Double, x2 As Double
  Dim z1 As Double, z2 As Double
  Dim pts As Variant
    
  x1 = xmin: x2 = xmax
  z1 = zmin: z2 = zmax

      pts = Array(x1, y, z1, x1, y, z2, x2, y, z2, x2, y, z1)
      Call draw_3D_array(pts)
      plane_pl g_3D_pline, strlayer
End Sub

Sub zplane(z As Double, strlayer As String)
  Dim x1 As Double, x2 As Double
  Dim y1 As Double, y2 As Double
  Dim pts As Variant
    
  x1 = xmin: x2 = xmax
  y1 = zmin: y2 = zmax

      pts = Array(x1, y1, z, x1, y2, z, x2, y2, z, x2, y1, z)
      Call draw_3D_array(pts)
      plane_pl g_3D_pline, strlayer
End Sub


Sub draw_3D_array(ar As Variant)
     Dim pts() As Double
     Dim i As Integer
     Dim lower As Integer, upper As Integer
     lower = LBound(ar)
     upper = UBound(ar)
     
     ReDim pts(lower To upper)
     For i = lower To upper
     pts(i) = ar(i)
     Next i
         
         Set g_3D_pline = acadDoc.ModelSpace.Add3DPoly(pts)
         g_3D_pline.Closed = True
         g_3D_pline.Update
End Sub

Sub plane_pl(pline As Acad3DPolyline, Optional strlayer As Variant)
 Dim lines(0) As AcadEntity
 Set lines(0) = pline

 Dim regionobj As Variant
 regionobj = acadDoc.ModelSpace.AddRegion(lines)
 regionobj(0).EntityTransparency = g_transparency

    If Not IsMissing(strlayer) Then
       regionobj(0).Layer = strlayer
    End If
End Sub

in 2D space, the standard equation of a line is

Ax + By + D = 0

in 3D space, this is a plane.

the standard equation of a plane in 3D space is

Ax + By + Cz + D = 0

if C is zero, the plane is parallel to the Z axis.

the equation can be rewritten to

Y = -A/B * X – D/B


Sub test_plane9()
  init

  xyplane -4, 2, 2, "4"
  xyplane 3, 2, -2, "5"
      
  Update
End Sub


Sub xyplane(A As Double, B As Double, D As Double, strlayer As String)
  Dim x1 As Double, x2 As Double
  Dim y1 As Double, y2 As Double
  Dim z1 As Double, z2 As Double
  Dim pts As Variant
    
  z1 = zmin: z2 = zmax
  x1 = xmin: x2 = xmax

  y1 = -A / B * x1 - (D / B)
  y2 = -A / B * x2 - (D / B)

      pts = Array(x1, y1, z1, x2, y2, z1, x2, y2, z2, x1, y1, z2)
      Call draw_3D_array(pts)
      plane_pl g_3D_pline, strlayer
End Sub

if you are not seeing transparency, there is a variable that disables it.

set TRANSPARENCYDISPLAY to 1

another variable DELOBJ deletes the defining lines when the Region is made. it works if the Region is made manually, but does not seem to work when the Region is made in code.

remember, set Visual Style not to 2D wireframe

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.