• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Distinguished Contributor
    vince1327
    Posts: 117
    Registered: ‎11-02-2011
    Accepted Solution

    Create rectangle using "AddVertexAt" using Point3d

    224 Views, 7 Replies
    05-16-2012 07:21 AM

    Hey everyone, yet another question...

     

    I've got my code to create a rectangle just fine, however i've noticed it doesn't take "Z" coordinates at all. I've got through the code and found my issue to be at these lines;

     

     BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    using (Polyline pline = new Polyline())
                    {
                        pline.AddVertexAt(0, new Point2d(LowerLeft.X, LowerLeft.Y), 0.0, 0.0, 0.0);
                        pline.AddVertexAt(1, new Point2d(UpperRight.X, LowerLeft.Y), 0.0, 0.0, 0.0);
                        pline.AddVertexAt(2, new Point2d(UpperRight.X, UpperRight.Y), 0.0, 0.0, 0.0);
                        pline.AddVertexAt(3, new Point2d(LowerLeft.X, UpperRight.Y), 0.0, 0.0, 0.0);
                        pline.Closed = true;
                        btr.AppendEntity(pline);
                        tr.AddNewlyCreatedDBObject(pline, true);
                    }

     I need to use a Point3d in order to add "Z" but apparently the pline.AddVertexAt only allows for a Point2d. Has any come across this or know of a solution?

     

    Cheers

    Vince

    Please use plain text.
    Mentor
    Posts: 214
    Registered: ‎04-11-2010

    Re: Create rectangle using "AddVertexAt" using Point3d

    05-16-2012 07:58 AM in reply to: vince1327

    Hi,

     

    Polyline has a elevation property, so pline.Elevation=Z it's what you need.

     

    Gaston Nunez

    Please use plain text.
    Distinguished Contributor
    vince1327
    Posts: 117
    Registered: ‎11-02-2011

    Re: Create rectangle using "AddVertexAt" using Point3d

    05-16-2012 08:04 AM in reply to: gasty1001

    Thanks gasty. any chance you have an example of this in action?

    Please use plain text.
    Distinguished Contributor
    vince1327
    Posts: 117
    Registered: ‎11-02-2011

    Re: Create rectangle using "AddVertexAt" using Point3d

    05-16-2012 08:40 AM in reply to: vince1327

    Actually scratch that, I still need to use a 3dPolyline as I will have differing start and end elevations. Is it possible to modify the above code to use a 3dPolyline?

     

     

    Thanks Again

    Vince

    Please use plain text.
    Mentor
    Posts: 214
    Registered: ‎04-11-2010

    Re: Create rectangle using "AddVertexAt" using Point3d

    05-16-2012 09:20 AM in reply to: vince1327

    Hi,

     

    This will draw a rectangle with a elevation of 400

     

        <CommandMethod("DREC")> _
        Public Sub DrawRect()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = HostApplicationServices.WorkingDatabase()
            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            Dim p1, p2 As Point3d
    
            Using acTrans As Transaction = db.TransactionManager.StartTransaction()
                Dim acSSPrompt As PromptPointResult = doc.Editor.GetPoint("Select First Point: ")
                If acSSPrompt.Status = PromptStatus.OK Then
                    p1 = acSSPrompt.Value
                    acSSPrompt = doc.Editor.GetCorner(vbCrLf + "Select Second Point: ", p1)
                    If acSSPrompt.Status = PromptStatus.OK Then
                        p2 = acSSPrompt.Value
                    Else
                        MsgBox("Invalid point")
                        Exit Sub
                    End If
                Else
                    MsgBox("Invalid point")
                    Exit Sub
                End If
    
                Dim v1 As Vector3d
                Dim theta As Double
                Dim p3, p4 As TVertex
                Dim a, b, l, s As Double
                Dim d As Double
    
                v1 = p1.GetVectorTo(p2)
                theta = v1.GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis.Negate())
                d = p1.DistanceTo(p2)
                a = d * Math.Cos(theta)
                b = d * Math.Sin(theta)
                l = 2 * (Math.Abs(a) + Math.Abs(b))
                s = Math.Abs(a * b)
    
                If s <= 0.001 Then
                    MsgBox("Degenerated Rectangle, try again", vbCritical)
                    Exit Sub
                End If
    
                p3 = New Point3d(p1.X + a, p1.Y, p1.Z)
                p4 = New Point3d(p1.X, p1.Y + b, p1.Z)
    
                Dim pl As New Polyline
    
                pl.AddVertexAt(0, New Point2d(p1.X, p1.Y), 0, 0, 0)
                pl.AddVertexAt(1, New Point2d(p3.X, p3.Y), 0, 0, 0)
                pl.AddVertexAt(2, New Point2d(p2.X, p2.Y), 0, 0, 0)
                pl.AddVertexAt(3, New Point2d(p4.X, p4.Y), 0, 0, 0)
                pl.Closed = True
                pl.Elevation = 400 '<---------------------Elevation
    
                Dim acBlkTbl As BlockTable
                acBlkTbl = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead)
                Dim acBlkTblRec As BlockTableRecord
                acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
    
                acBlkTblRec.AppendEntity(pl)
                acTrans.AddNewlyCreatedDBObject(pl, True)
    
                ed.WriteMessage(vbCrLf + "Rectangle length:=" + l.ToString + vbCrLf)
                ed.WriteMessage("Rectangle Area:=" + s.ToString + vbCrLf)
                ed.WriteMessage("Width:=" + Math.Abs(a).ToString + vbCrLf)
                ed.WriteMessage("Height:=" + Math.Abs(b).ToString + vbCrLf)
    
                acTrans.Commit()
            End Using
        End Sub

     

    Gaston Nunez

    Please use plain text.
    Distinguished Contributor
    vince1327
    Posts: 117
    Registered: ‎11-02-2011

    Re: Create rectangle using "AddVertexAt" using Point3d

    05-16-2012 09:48 AM in reply to: gasty1001

    Ahh that makes much more sense. Is it possible to create the vertexes using Point3D so that each vertex may be given a unique "Z"? I've been playing around with the InsertVertexAt properties of Point3D with no luck.

     

    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    using (Polyline pline = new Polyline())      
                   // using (Polyline3d pline3d = new Polyline3d())
                  //  using (PolylineVertex3d vertex = new PolylineVertex3d())
                    
                    {                
                        
                      //  pline3d.InsertVertexAt (0,);
    
                        pline.AddVertexAt(0, new Point2d(LowerLeft.X, LowerLeft.Y), 0.0, 0.0, 0.0);
                        pline.AddVertexAt(1, new Point2d(UpperRight.X, LowerLeft.Y), 0.0, 0.0, 0.0);
                        pline.AddVertexAt(2, new Point2d(UpperRight.X, UpperRight.Y), 0.0, 0.0, 0.0);
                        pline.AddVertexAt(3, new Point2d(LowerLeft.X, UpperRight.Y), 0.0, 0.0, 0.0);
                        pline.Closed = true;
                        pline.Elevation = LowerLeft.Z;
                        btr.AppendEntity(pline);
                        tr.AddNewlyCreatedDBObject(pline, true);
                    }
                    tr.Commit();
                }
            }

     

     

     

     

    Please use plain text.
    *Expert Elite*
    chiefbraincloud
    Posts: 736
    Registered: ‎02-13-2008

    Re: Create rectangle using "AddVertexAt" using Point3d

    05-16-2012 10:19 AM in reply to: vince1327

    For a Polyline3d you either pass a Point3dCollection of your vertex coordinates into the constructor, or you call the constructor with no arguments and use

    PLObj.AppendVertex(New PolylineVertex3d(LowerLeft))

    and so on...

     

    Depending on your specific needs, you potentially have two other options.  You could still use a Polyline, and use TransformBy to rotate it around the x and/or y axis to get the proper orientation, or you could set the Normal property to accomplish the same thing.  A Polyline must be planar, but it does not have to be parallel to the XY plane.

     

    I'm thinking the easiest route is to use a Polyline3d, and as long as you aren't making thousands of them, the additional overhead shouldn't be a problem.

    Dave O.                                                                                Sig-Logos32.png
    Please use plain text.
    Mentor
    Posts: 214
    Registered: ‎04-11-2010

    Re: Create rectangle using "AddVertexAt" using Point3d

    05-16-2012 10:21 AM in reply to: vince1327

    Hi,

     

    No, you can't add a Point3D as a vertex to a Polyline,  i fyou need a "sloped" rectangle, you have to setup the plane (UCS)  on that the Polyline will lie on, and that, I think,  requires some Matrix3d manipulation. You can read this post on Theswamp: http://www.theswamp.org/index.php?topic=41571.0, I have no time now to code but i'll check this topic.

    later.

     

    Any way, if you need to put the vertex list of a Polyline ina Point3dCollection, it's simple:

     

    Dim n as integer=Pline.NumberOfVertices-1

    Dim i as integer

    Dim pc as new Point3dCollection

    Dim vertexList as new list (of Point3D)

     

    For i=Pline.StartParameter to n

     pc.Add(Pline.GetPointAtparaMeter(i))

     vertexList .Add(Pline.GetPointAtparaMeter(i))

    Next

     

    Note1:I'd prefer to use a list instead of Point3dCollection in most cases.

    Note2: The GetPointAtParameter returns a Point3D with Z=Elevation,

     

     

    Gaston Nunez

     

    Please use plain text.