saving rectangle vertex coordinates to access database

saving rectangle vertex coordinates to access database

Anonymous
Not applicable
717 Views
3 Replies
Message 1 of 4

saving rectangle vertex coordinates to access database

Anonymous
Not applicable

i have method for creating rectangle:

Public Shared Sub PlotRectangle()
            Dim doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db = doc.Database
            Dim ed = doc.Editor
  
            Dim ppr = ed.GetPoint(vbLf & "First corner: ")
            If ppr.Status <> PromptStatus.OK Then Return
            Dim pt1 = ppr.Value
            ppr = ed.GetCorner(vbLf & "Opposite corner: ", pt1)
            If ppr.Status <> PromptStatus.OK Then Return
            Dim pt2 = ppr.Value
            
            Dim x1  = pt1.X
            Dim y1  = pt1.Y
            Dim x2  = pt2.X
            Dim y2  = pt2.Y
            
            Using locked As DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument()
            Using tr = db.TransactionManager.StartTransaction()
                Dim pline = New Polyline(4)
                
                pline.AddVertexAt(0, New Point2d(x1, y1), 0, 0, 0)
                pline.AddVertexAt(1, New Point2d(x2, y1), 0, 0, 0)
                pline.AddVertexAt(2, New Point2d(x2, y2), 0, 0, 0)
                pline.AddVertexAt(3, New Point2d(x1, y2), 0, 0, 0)
                pline.Closed = True
                pline.TransformBy(ed.CurrentUserCoordinateSystem)
                Dim curSpace = CType(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                
                curSpace.AppendEntity(pline)
                SaveRectangle(pline,Username,pline.Handle)
                tr.AddNewlyCreatedDBObject(pline, True)
                tr.Commit()
            End Using
                End Using

and have uncompleted method to save coordinates of rectangle(upperleft, upperright, lowerleft, lowerright) to Database.

Public Shared Sub SaveRectangle(ByVal rectEnt As Rectangle3d, ByVal username As String, GeomId As Handle)
            Dim groupid as Integer
            Dim ObjectsInGroup As List(Of Handle) = New List(Of Handle)
            Dim sqlSelectObjIds As String = "SELECT ENT_ID FROM ObjectIds"
            Dim sqlSelect As String = "SELECT GROUP_ID from Groups"
            Dim sqlQryGrp As String = "INSERT INTO Groups (GROUP_ID,Group_name) VALUES (pGroup_id,pGroup_name)"
            Dim sqlQryEnt As String = "INSERT INTO EntityParams (Geom_type, CREATED_BY, UPDATED_BY) VALUES (pGeom_type,pCreated,pUpdated)"
            Dim sqlQryEntInGroups As String = "INSERT INTO EntitiesInGroups (GROUP_ID, ENT_ID) VALUES (pGroup_id,pEnt_id)"
            Dim sqlQryRect As String = "INSERT INTO RectangleParams (ENT_ID, CoordinateX1, CoordinateX2, CoordinateY1, CoordinateY2) VALUES (pENT_ID, pCoordinateX1, pCoordinateX2, pCoordinateY1,pCoordinateY2)"
            Dim myconnection As New OleDbConnection(ConnectionString)
            Dim cmd As New OleDbCommand(sqlSelect, myconnection)
            

            Using myconnection
                myconnection.Open()
                Using cmd
                    Using cmd
                        cmd.Parameters.AddWithValue("pGroup_name",groupName)
                        If cmd.ExecuteScalar IsNot Nothing Then
                            groupId = cmd.ExecuteScalar()
                        End If
                    End Using
              

            End Using
           
            cmd.CommandText = sqlQryEnt
                cmd.Connection = myconnection
                Using cmd
                    cmd.Parameters.AddWithValue("pGeom_type", "Rectangle")
                    cmd.Parameters.AddWithValue("pCreated", username)
                    cmd.Parameters.AddWithValue("pUpdated", username)
                    cmd.ExecuteNonQuery()
                    cmd.CommandText = "SELECT @@IDENTITY"
                    EntId = cmd.ExecuteScalar()
                End Using
               
                
                cmd.CommandText = sqlQryRect
                cmd.Connection = myconnection
                Using cmd
                    cmd.Parameters.AddWithValue("pENT_ID", EntId)
                    cmd.Parameters.AddWithValue("pCenterX", rectEnt.LowerLeft)
                    cmd.ExecuteNonQuery()
                End Using
            End Using
            
            myconnection.Close()
            DictAutoCadIdToDbId.Add(GeomId.Value(), EntId)
            ObjectsInGroup.Add(GeomId)
            AddToGroup(groupName,GeomId)
            RightPal.RefreshTree()
        End Sub

Pline type is Polyline but i need it to Rectangle3d to be able to save all 4 vertex coordinates any suggestions how to convert polyline to rectangle3d or smth

0 Likes
Accepted solutions (1)
718 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

If you look at Rectangle3d class' Contructor, it has 4 rectagle's corner points as arguments.

 

In your first method PlotRectangle() (it sound strnange to any AutoCAD user for calling the process of adding entity as "Plot"Smiley Happy), you are able to get 4 points and draw a rectangle as Polyline. So, you should no problem to simply ccreate a Rectangle3d struct and use it in the other method SaveRectangle().

 

You may slightly change the method signature of PlotRectangle, so it not only draws the rectangle as polyline, but also returns a Rectangle3d struct for the next method to use. Something like:

 

Public Shared Function PlotRectangle() As rectangle3d

 

   '' Pick 2 points as rectangle's opposite corners

   '' Define 4 points (uperleft, upperright, lowerleft and lowerright)

   Dim rect As Rectangle3d = New rectangle3d(upperleft, upperright, lowerleft, lowerright)

 

   '' draw polyline in drawing database according to the 4 points

 

   '' Finally returns the Rectangle3d struct

   Return rect

 

End Function

 

Then in your main process code, 

 

....

Dim rect As Rectangle3d = PlotRectangle()

SaveRectangle(rect, ...)

 

As you can see, it is NOT that method SaveRectangle() incomplete, it is the PlotRectangle() should be able to pass/return useful information out for the next operation's convenience

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

Anonymous
Not applicable

yeah i got what u say but problem is that x1, y1,x2,y2 are double but upperleft, upperright, ... are point3d type so literally i cant create rect as Rectangle3d

0 Likes
Message 4 of 4

_gile
Consultant
Consultant
Accepted solution

While having pt1 and pt2, you can build an Extens3d instance which have a MinPoint (lower left) and MaxPoint (upper right) properties to help you creation your instance of Rectangle3d.

 

var extents = new Extents3d();
extents.AddPoint(pt1);
extents.AddPoint(pt2);
var minX = extents.MinPoint.X;
var minY = extents.MinPoint.Y;
var maxX = extents.MaxPoint.X;
var maxY = extents.MaxPoint.Y;
var z = pt1.Z;
var rectangle =  new Rectangle3d(
	new Point3d(minX, maxY, z),
	new Point3d(maxX, maxY, z),
	new Point3d(minX, minY, z),
	new Point3d(maxX, minY, z));

 

Keep in mind pt1 and pt2 are defined in the current coordinate system which may be different from WCS.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub