Author Topic: Label polyline areas with field  (Read 1099 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Label polyline areas with field
« on: May 18, 2012, 01:50:27 PM »
Code: [Select]
//----------------------------------------------------------------------------------------------//
        [CommandMethod("POLA", CommandFlags.Modal)]
        public void PolyAreas()
        {
            var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            Matrix3d ucs = ed.CurrentUserCoordinateSystem;
           
            var sf = new SelectionFilter(new TypedValue[] { new TypedValue(0, "LWPOLYLINE"), new TypedValue(70, 1) });
            var pso = new PromptSelectionOptions();
            pso.MessageForAdding = "Select polylines (or enter for all): ";
            pso.AllowDuplicates = false;
            var psr = ed.GetSelection(pso, sf);
            // If nothing selected then select all
            if (psr.Status == PromptStatus.Error) psr = ed.SelectAll(sf);
            // make sure the selection isn't empty
            if (psr.Status == PromptStatus.OK && psr.Value.Count > 0)
            {
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    var btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    foreach (SelectedObject so in psr.Value)
                    {
                        var pline = (Polyline)tr.GetObject(so.ObjectId, OpenMode.ForRead);

                        Point3d cp = GetPseudoCentroid(db, pline).TransformBy(ucs);
                        string strObjId = pline.ObjectId.ToString();
                        strObjId = strObjId.Trim(new char[] { '(', ')' });
                        var mtx = new MText();
                        mtx.Contents = "%<\\AcObjProp Object(%<\\_ObjId " + strObjId + ">%).Area \\f \"%pr3%lu2%ct4%qf1 SQ. FT.\">%";
                        mtx.Location = cp;
                        mtx.Rotation = 0;
                        mtx.Attachment = AttachmentPoint.MiddleCenter;

                        btr.AppendEntity(mtx);
                        tr.AddNewlyCreatedDBObject(mtx, true);
                        tr.TransactionManager.QueueForGraphicsFlush();

                    }
                    doc.TransactionManager.FlushGraphics();
                    tr.Commit();
                    ed.Regen();
                }
            }
        }

        //----------------------------------------------------------------------------------------------//
        public Point3d GetPseudoCentroid(Database db, Polyline pline)
        {
            Point3d cpt = new Point3d();
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            try
            {
                using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
                {

                    Point3d maxp = pline.GeometricExtents.MaxPoint;
                    Point3d minp = pline.GeometricExtents.MinPoint;
                    cpt = new Point3d((maxp.X + minp.X) / 2, (maxp.Y + minp.Y) / 2, (maxp.Z + minp.Z) / 2);

                    tr.Commit();
                    return cpt;
                }
            }
            catch
            {
                // ed.WriteMessage("\nError on getting centroid");//debug only
                return cpt;
            }

        }

        //----------------------------------------------------------------------------------------------//

~'J'~

Offline m4rdy

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Re: Label polyline areas with field
« Reply #1 on: June 30, 2012, 11:28:36 AM »
Hai Oleg,
I'm newbie here and i still learning dotnet.
I have copied your code above, and i have difficult to make dll file.
I use C# VS2005 express and autocad 2007.
Please give me any advices.
Thank you.

mardi

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Re: Label polyline areas with field
« Reply #2 on: June 30, 2012, 04:43:13 PM »
Hi, Mardy,

Let my a time to try variables relatively to A2008,

~'J'~

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Re: Label polyline areas with field
« Reply #3 on: June 30, 2012, 04:53:30 PM »
Mardy, try again please

Code: [Select]
        //----------------------------------------------------------------------------------------------//
                [CommandMethod("POLA", CommandFlags.Modal)]   
        public void PolyAreas()
                { 
                    Document  doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; 
                    Database  db = doc.Database;       
                    Editor  ed = doc.Editor;         
                    Matrix3d ucs = ed.CurrentUserCoordinateSystem;
                    SelectionFilter sf = new SelectionFilter(new TypedValue[] { new TypedValue(0, "LWPOLYLINE"), new TypedValue(70, 1) });
                    PromptSelectionOptions pso = new PromptSelectionOptions();       
                    pso.MessageForAdding = "Select polylines (or enter for all): ";   
                    pso.AllowDuplicates = false;
                    PromptSelectionResult psr = ed.GetSelection(pso, sf);     
                    // If nothing selected then select all   
                    if (psr.Status == PromptStatus.Error) psr = ed.SelectAll(sf);
                    // make sure the selection isn't empty     
                    if (psr.Status == PromptStatus.OK && psr.Value.Count > 0)   
                    {
                        using (Transaction tr = db.TransactionManager.StartTransaction()) 
                        {
                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); 
                            foreach (SelectedObject so in psr.Value)     
                            {
                                Polyline pline = (Polyline)tr.GetObject(so.ObjectId, OpenMode.ForRead);     
                                Point3d cp = GetPseudoCentroid(db, pline).TransformBy(ucs);   
                                string strObjId = pline.ObjectId.ToString();   
                                strObjId = strObjId.Trim(new char[] { '(', ')' });
                                MText mtx = new MText();               
                                mtx.Contents = "%<\\AcObjProp Object(%<\\_ObjId " + strObjId + ">%).Area \\f \"%pr3%lu2%ct4%qf1 SQ. FT.\">%";                        mtx.Location = cp;             
                                mtx.Rotation = 0;     
                                mtx.Attachment = AttachmentPoint.MiddleCenter;   
                                btr.AppendEntity(mtx);           
                                tr.AddNewlyCreatedDBObject(mtx, true);             
                                tr.TransactionManager.QueueForGraphicsFlush();       
                            }         
                            doc.TransactionManager.FlushGraphics(); 
                            tr.Commit();         
                            ed.Regen();         
                        }     
                    }     
                }     
        //----------------------------------------------------------------------------------------------//   
        public Point3d GetPseudoCentroid(Database db, Polyline pline)   
        {           
            Point3d cpt = new Point3d();   
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            try         
            {         
                using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())     
                {           
                    Point3d maxp = pline.GeometricExtents.MaxPoint;   
                    Point3d minp = pline.GeometricExtents.MinPoint;     
                    cpt = new Point3d((maxp.X + minp.X) / 2, (maxp.Y + minp.Y) / 2, (maxp.Z + minp.Z) / 2); 
                    tr.Commit();       
                    return cpt;         
                }         
            }       
            catch       
            {     
                ed.WriteMessage("\nError on getting centroid");//debug only   
                return cpt;     
            }     
        }
        //----------------------------------------------------------------------------------------------// 


~'J'~

Offline m4rdy

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Re: Label polyline areas with field
« Reply #4 on: June 30, 2012, 06:56:58 PM »
Hai Oleg,
Its working now, with minor change.
I just change this:
 //using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())

To:
using (Transaction tr = db.TransactionManager.StartTransaction())

Thanks for your help.

mardi