Convert DBObjectCollection to polyline

Convert DBObjectCollection to polyline

youssefGC
Advocate Advocate
1,272 Views
5 Replies
Message 1 of 6

Convert DBObjectCollection to polyline

youssefGC
Advocate
Advocate

Hello Forums,

I'm trying to work with "GetOffsetCurves()"  in order to create a offset polyline(opened), but it return a DBObjectCollection. To convert this collections, I put a function :

 

public Polyline ConvertDBObjectToPolyline(Document doc, DBObjectCollection dbcol, Plane _plane)
        {
            Polyline _Pline = new Polyline();
            Database db = doc.Database;

            Transaction tr =doc.TransactionManager.StartTransaction();
            using (tr)
            {
                BlockTable bt =(BlockTable)tr.GetObject(db.BlockTableId,OpenMode.ForWrite);
                BlockTableRecord btr =(BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                // For initial Curve take the first in the list
                Curve cv1 = dbcol[0] as Curve;

                _Pline.AddVertexAt(_Pline.NumberOfVertices, cv1.StartPoint.Convert2d(_plane), BulgeFromCurve(cv1, false), 0, 0);
                _Pline.AddVertexAt(_Pline.NumberOfVertices, cv1.EndPoint.Convert2d(_plane), 0, 0, 0);
                dbcol.Remove(cv1);

                Point3d nextPt = cv1.EndPoint;
                int prevCnt = dbcol.Count + 1;

                while (dbcol.Count > 0 && dbcol.Count < prevCnt)
                {
                    prevCnt = dbcol.Count;
                    foreach (Curve cv in dbcol)
                    {
                        // If one end of the curve connects with the
                        // point we're looking for...
                        if (cv.StartPoint == nextPt || cv.EndPoint == nextPt)
                        {
                            // Calculate the bulge for the curve and
                            // set it on the previous vertex
                            double bulge = BulgeFromCurve(cv,false);
                            _Pline.SetBulgeAt(_Pline.NumberOfVertices - 1, bulge);

                            // Reverse the points, if needed
                            if (cv.StartPoint == nextPt)
                                nextPt = cv.EndPoint;
                            else
                                // cv.EndPoint == nextPt
                                nextPt = cv.StartPoint;

                            // Add out new vertex (bulge will be set next
                            // time through, as needed)
                            _Pline.AddVertexAt(_Pline.NumberOfVertices, nextPt.Convert2d(_plane), 0, 0, 0);

                            // Remove our curve from the list, which
                            // decrements the count, of course
                            dbcol.Remove(cv);

                            break;
                        }
                    }
                }

                tr.Commit();
            }

            return _Pline;
        }

 

After debugging, it's doesn't march in all segments!

 

0 Likes
Accepted solutions (2)
1,273 Views
5 Replies
Replies (5)
Message 2 of 6

Jeff_M
Consultant
Consultant
Accepted solution

The DBObjectCollection from an offset polyline should only contain 1 object, a Polyline.

//crv is a Polyline object
var offset = crv.GetOffsetCurves(15.0);
var pline = (Polyline)offset[0];
Jeff_M, also a frequent Swamper
EESignature
Message 3 of 6

_gile
Consultant
Consultant
Accepted solution

Hi,

More accurately, the GetOffsetCurves method returns a DBObjectCollection because offesting a curve may generate more than one curve. If the the source curve is a polyline, the generated curves are also polylines

(view in My Videos)

 

Your ConvertDBObjectToPolyline method could be simply like this:

 

        public void ConvertDBObjectToPolyline(Database db, DBObjectCollection dbCol)
        {
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var modelSpace = (BlockTableRecord)tr.GetObject(
                    SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
                foreach(Curve curve in dbCol)
                {
                    modelSpace.AppendEntity(curve);
                    tr.AddNewlyCreatedDBObject(curve, true);
                }
                tr.Commit();
            }
        }

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 6

youssefGC
Advocate
Advocate
That works, thanks a lot
0 Likes
Message 5 of 6

youssefGC
Advocate
Advocate
Yes, but I want to retrieve it as a polyline
0 Likes
Message 6 of 6

Jeff_M
Consultant
Consultant

@_gile correct, I knew there were cases where there may be multiples I just couldn't think of any examples last night. Thanks for the correction.

Jeff_M, also a frequent Swamper
EESignature
0 Likes