PDA

View Full Version : drawing a polygon in space



MikeJarosz
2007-12-04, 09:56 PM
I'm back from AU and 5 classes with the great Jerry Winters. Totally exhausted. AND, it rained on the last day. It never rains in LV!!!!!!!

Anyway, I have been given an Excel file of an exterior roof design that resembles a crumpled sheet of aluminum foil. The roof has been triangulated and the 9 points needed to generate each polyline are given. Here is an example of one triangle (out of 3,012):

34.1642,55.0000,-0.0714
34.7968,56.1000,0.0211
33.5315,56.1000,0.0524

Note that all have three different non-zero z coords. To generate a polyline I use this code:


Dim Polygon As AcadLWPolyline
Set Polygon = ThisDrawing.ModelSpace.AddLightWeightPolyline(Vertices)

The trouble is, lightweight polylines ignore the z coordinate and enter the polyline using only the x and y. The manual then advises to use the TranslateCoordinates method. I can get it to draw the projection of the pline on a flat plane, but I need the 3D crumpled effect. I will also need to hatch the resulting pline according to a certain color scheme.

I really don't understand what is going on here. Using the 9 points above, could someone out there show me how to get a hatchable polyline viewable from any angle?

Ed Jobe
2007-12-04, 10:14 PM
2D polylines have to be drawn coplanar to the current ucs. You can either make the ucs planar to the desired triangle, or add a 3DPolyline. If you use the latter, you will have to make the ucs planar to that object in order to hatch it.

seant61
2007-12-05, 10:18 AM
This routine uses a similar procedure as outlined by Mr. Jobe. In this case, the current UCS remains as WCS and both Pline and Hatch are reoriented subsequent to creation.

The attached DWG shows the resultant Pline and Hatch (colors were altered from routine results to assist demo). Points were added at the vertices to compare their location to world. Notice how the Z value of the points differs quite a bit from the Pline’s elevation.

This demonstrates how AutoCAD interprets an objects OCS. If the plane of the pline were expanded infinitely, and a line were drawn from the WCS 0,0,0 perpendicular to that plane, the lines length would be the Elevation value, and the direction would be equal to the objects Normal.


Option Explicit

Sub Main()
Dim dblPt1(0 To 2) As Double
Dim dblPt2(0 To 2) As Double
Dim dblPt3(0 To 2) As Double
dblPt1(0) = 34.1642: dblPt1(1) = 55#: dblPt1(2) = -0.0714
dblPt2(0) = 34.7968: dblPt2(1) = 56.1: dblPt2(2) = 0.0211
dblPt3(0) = 33.5315: dblPt3(1) = 56.1: dblPt3(2) = 0.0524
GenPLfrom3Pts dblPt1, dblPt2, dblPt3
End Sub

Private Sub GenPLfrom3Pts(Pt1() As Double, Pt2() As Double, Pt3() As Double)
Dim dblVector1() As Double
Dim dblvector2() As Double
Dim dblNormalVector() As Double
Dim dblTransPt1() As Double
Dim dblTransPt2() As Double
Dim dblTransPt3() As Double
Dim dblElevation As Double
Dim dblVertlist(0 To 5) As Double
Dim entLWPline As AcadLWPolyline
Dim entHatch As AcadHatch
Dim entOuterLoop(0 To 0) As AcadEntity
Dim i As Long
With ThisDrawing
dblVector1 = VectorFrom2PtsST(Pt1, Pt2)
dblvector2 = VectorFrom2PtsST(Pt2, Pt3)
dblNormalVector = VectorCrossST(dblVector1, dblvector2)
dblTransPt1 = .Utility.TranslateCoordinates(Pt1, acWorld, acOCS, 0, dblNormalVector)
dblTransPt2 = .Utility.TranslateCoordinates(Pt2, acWorld, acOCS, 0, dblNormalVector)
dblTransPt3 = .Utility.TranslateCoordinates(Pt3, acWorld, acOCS, 0, dblNormalVector)
dblElevation = dblTransPt1(2)
For i = 0 To 1
dblVertlist(i) = dblTransPt1(i)
dblVertlist(i + 2) = dblTransPt2(i)
dblVertlist(i + 4) = dblTransPt3(i)
Next
Set entLWPline = .ModelSpace.AddLightWeightPolyline(dblVertlist)
entLWPline.Closed = True
Set entOuterLoop(0) = entLWPline
Set entHatch = GenHatch()
entHatch.AppendOuterLoop (entOuterLoop)
entHatch.Evaluate
entLWPline.Elevation = dblElevation
entHatch.Elevation = dblElevation
entLWPline.Normal = dblNormalVector
entHatch.Normal = dblNormalVector
End With
End Sub

Function VectorFrom2PtsST(dbl1stPt() As Double, dbl2ndPt() As Double) As Double()
Dim dblDummy(0 To 2) As Double
dblDummy(0) = dbl2ndPt(0) - dbl1stPt(0)
dblDummy(1) = dbl2ndPt(1) - dbl1stPt(1)
dblDummy(2) = dbl2ndPt(2) - dbl1stPt(2)
VectorFrom2PtsST = dblDummy
End Function

Public Function VectorCrossST(dblVect1() As Double, dblVect2() As Double) As Double()
Dim dblDummy(0 To 2) As Double
dblDummy(0) = dblVect1(1) * dblVect2(2) - dblVect1(2) * dblVect2(1)
dblDummy(1) = dblVect1(2) * dblVect2(0) - dblVect1(0) * dblVect2(2)
dblDummy(2) = dblVect1(0) * dblVect2(1) - dblVect1(1) * dblVect2(0)
VectorCrossST = dblDummy
End Function

Function GenHatch() As AcadHatch
'mostly from AddHatch Example
Dim patternName As String
Dim PatternType As Long
Dim bAssociativity As Boolean
patternName = "Solid"
PatternType = acHatchPatternTypePreDefined
bAssociativity = True
Set GenHatch = ThisDrawing.ModelSpace.AddHatch(PatternType, patternName, bAssociativity)
End Function

MikeJarosz
2007-12-05, 07:59 PM
Sean:

It worked perfectly. I wrote a loop that read the 3012 lines are here is the result. (I turned off the fill)

seant61
2007-12-05, 10:51 PM
Damn, that's a cool roof. Should I hesitate to call it Gehry-esque?

In any event, I'm glad I could help.

Sean

dgorsman
2007-12-06, 07:55 PM
NICE. I may need to borrow that idea for something else....

seant61
2007-12-07, 12:45 AM
NICE. I may need to borrow that idea for something else....

Can you elaborate?

dgorsman
2007-12-10, 07:53 PM
Noting work related, just something I used a brute force algorithm for a few years earlier. In this case, applying a surface to a random map generated off of a hexagonal grid.