.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

smart counter

2 REPLIES 2
Reply
Message 1 of 3
KrisP007
353 Views, 2 Replies

smart counter

hi ,

I want to create a counter that also knows in witch zone(area) it is.

for example I have a zone A,B,C and D when i insert my bloc the value off the attribute has a prefix of 01. (floor) the Zone it's in and the last nr +1.

I'm working in AutoCAD 2014 and C#.net.

regards,

Kris.

 

2 REPLIES 2
Message 2 of 3
hgasty1001
in reply to: KrisP007

Hi,

 

If the test area is always the same as your drawing, you can decompose the non rectangular zones in rectangles, get the coordinates of each rectangle, and test the insertion point of the block against each rectangle, its just a few comparison against the min x, min y and max x, max y of the rectangle (iow its extents). If this "easy" approach doesn't work for any reason, you can try with Editor.TraceBoundary() method, it accept a test point as argument, and returns a dbObjectCollection, then yo have to check the coordinates of the curve objects in the collection(if empty the test point it's outside of any boundary) to discriminate the zone you need. There is another method with Brep, check this recent post: Spatial Query .

 

Gaston Nunez

Message 3 of 3
KrisP007
in reply to: KrisP007

This is what i have so far.

 

I'm new at .net so i don't know if it's clean code.

 

type "Monitor" to start

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System.Collections.Generic;
using System.Collections;
using System;
using System.Reflection;
using Microsoft.VisualBasic;


[assembly: CommandClass(typeof(SmartCounter1.Monitor))]
[assembly: CommandClass(typeof(SmartCounter1.Areas))]

namespace SmartCounter1
{
    public class Monitor
    {
        [CommandMethod("Monitor")] // command to start 

        // is there a Area definded        
        public void MonitorMain()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Areas area = new Areas();
            if (area.HasArea())// if true then go ,else define area first.
            {
                ed.WriteMessage("\narea's are ok");
                Areas.StartMonitor();
            }
            else
            {
                //define area
                ed.WriteMessage("\narea's are not ok");
                ed.WriteMessage("\ndefine Area's first");
                Areas.StopMonitor();

            }
        }
    }

    public class colPolypoints
    {

        public Point2d colPLpoint { get; set; }
        public static List<colPolypoints> colPolylist = new List<colPolypoints>();

        public static void AddcolPolypoints(Point2d _PLpoint)
        {
            colPolylist.Add(new colPolypoints {colPLpoint = _PLpoint });
        }

        public static void clearcolPolylist()
        {
            colPolylist.Clear();
        }

    }

    public class Polypoints
    {
       
        public string PLname { get; set; }
        public int PLpointnr { get; set; }
        public Point2d PLpoint { get; set; }
        public static List<Polypoints> Polylist = new List<Polypoints>();

        public static void AddPolypoints(string _PLname, int _PLpointnr, Point2d _PLpoint)
        {
             Polylist.Add(new Polypoints { PLname = _PLname, PLpointnr = _PLpointnr, PLpoint = _PLpoint });
        }
    }

    public class Areas : Polypoints
    {
        private static readonly List<string> _AreaList = new List<string>();
        public static IEnumerable<string> AreaList { get { return _AreaList; } }
        public static string MyZone { get; set; }
        public static string ActiveZone { get; set; }


        public bool HasArea()
        {
            GetAreaList();
            if (_AreaList.Count != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static int countarea()
        {
            int areacount = _AreaList.Count;
            return areacount;
        }

        internal static string getitem(int i)
        {
            string areaname = _AreaList[i];
            return areaname;
        }

        internal static List<colPolypoints> PLpoints(string Name)
        {
            List<colPolypoints> _PLpoints = new List<colPolypoints>();
            colPolypoints.clearcolPolylist();
            foreach (Polypoints point in Polypoints.Polylist)
            {
                if (point.PLname == Name)
                {
                    var _Pointnr = point.PLpointnr;
                    var _Point = point.PLpoint;
                    colPolypoints.AddcolPolypoints(_Point);
                }
                _PLpoints = colPolypoints.colPolylist;
            }
            return _PLpoints;
        }

        [CommandMethod("GetAreaList")]
        public void GetAreaList()
        {
            _AreaList.Clear();
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
            Transaction tr = db.TransactionManager.StartTransaction();
            using (tr)
            {
                try
                {
                    TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "LWPOLYLINE") };
                    SelectionFilter filter = new SelectionFilter(filList);
                    PromptSelectionResult selRes = ed.SelectAll(filter);
                    if (selRes.Status != PromptStatus.OK && selRes.Value.Count == 0)
                    {
                        ed.WriteMessage("\nerror in getting the selectAll");
                        return;
                    }
                    foreach (SelectedObject o in selRes.Value)
                    {
                        DBObject pol = tr.GetObject(o.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
                        ResultBuffer rb = pol.XData;
                        if (rb != null)
                        {
                            foreach (TypedValue tv in rb)
                            {
                                if (tv.TypeCode == 1000)
                                {
                                    Areas.MyZone = tv.Value.ToString();
                                    _AreaList.Add(tv.Value.ToString());
                                    ed.WriteMessage(" \nZone :{0}", Areas.MyZone);
                                }
                            }
                            Polyline Pl = pol as Polyline;
                            int vn = Pl.NumberOfVertices;
                            for (int i = 0; i < vn; i++)
                            {
                                Point2d pt = Pl.GetPoint2dAt(i);
                                Polypoints.AddPolypoints(Areas.MyZone, i, pt);
                            }
                            rb.Dispose();
                        }

                    }

                }
                catch
                {
                    ed.WriteMessage("error in GetAreaList");
                }
                tr.Commit();
            }
        }

        [CommandMethod("DefineA")]
        static public void SetAreaXData()
        {

            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            // Ask the user to select an entity for which to set XData
            string myValue = Interaction.InputBox("Enter Area Name.", "Area", string.Empty, -1, -1);
            if (myValue == "")
            {

                // to do 
            }
            else
            {
                // To Do check if value exist
                PromptEntityOptions opt = new PromptEntityOptions("\nSelect Area(Closed polyline): ");
                PromptEntityResult res = ed.GetEntity(opt);
                if (res.Status == PromptStatus.OK)
                {
                    Transaction tr = doc.TransactionManager.StartTransaction();
                    using (tr)
                    {
                        DBObject obj = tr.GetObject(res.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite);
                        AddRegAppTableRecord("Areas");
                        ResultBuffer rb = new ResultBuffer(new TypedValue(1001, "Areas"), new TypedValue(1000, myValue));
                        obj.XData = rb;
                        rb.Dispose();
                        tr.Commit();
                    }

                }
            }

        }

        static void AddRegAppTableRecord(string regAppName)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            Transaction tr = doc.TransactionManager.StartTransaction();
            using (tr)
            {
                RegAppTable rat = (RegAppTable)tr.GetObject(db.RegAppTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead, false);
                if (!rat.Has(regAppName))
                {
                    rat.UpgradeOpen();
                    RegAppTableRecord ratr = new RegAppTableRecord();
                    ratr.Name = regAppName;
                    rat.Add(ratr);
                    tr.AddNewlyCreatedDBObject(ratr, true);
                }
                tr.Commit();
            }
        }

        public static bool IsPointInPolygon(List<colPolypoints> poly, Point2d point)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            bool isInside = false;
            try
            {
                for (int i = 0, j = poly.Count - 1; i < poly.Count; j = i++)
                {
                    if (((poly[i].colPLpoint.Y > point.Y) != (poly[j].colPLpoint.Y > point.Y)) && (point.X < (poly[j].colPLpoint.X - poly[i].colPLpoint.X) * (point.Y - poly[i].colPLpoint.Y) / (poly[j].colPLpoint.Y - poly[i].colPLpoint.Y) + poly[i].colPLpoint.X))
                    {
                        isInside = !isInside;
                    }
                }
            }
            catch
            {
                ed.WriteMessage("error in calcul");
            }
            return isInside;
        }

        [CommandMethod("SM")]
        public static void StartMonitor()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.PointMonitor += new PointMonitorEventHandler(ed_PointMonitor);
        }

        [CommandMethod("XM")]
        public static void StopMonitor()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.TurnForcedPickOn();
            ed.PointMonitor -= new PointMonitorEventHandler(ed_PointMonitor);
            // if zone  Highlighted turn off
            HighlightPoly("off");

        }

        static void ed_PointMonitor(object sender, PointMonitorEventArgs e)
        {
            Editor ed1 = Application.DocumentManager.MdiActiveDocument.Editor;
            Areas.ActiveZone = "";
            try
            {
                Editor ed = (Editor)sender;
                Document doc = ed.Document;
                Database db = HostApplicationServices.WorkingDatabase;
                Point3d mouse = e.Context.ComputedPoint;
                double mouseX = mouse.X;
                double mouseY = mouse.Y;
                Point2d Mpoint = new Point2d(mouseX, mouseY);

                // controle if point is in area's
                //count off areas
                int areacount = Areas.countarea();
                for (int i = 0; i < areacount; i++)
                {
                    string AreaName = Areas.getitem(i);
                    // make array of polyline
                    List<colPolypoints> PLLinePoints = new List<colPolypoints>();
                    PLLinePoints = Areas.PLpoints(AreaName);
                    if (Areas.IsPointInPolygon(PLLinePoints, Mpoint))
                    {
                        Areas.ActiveZone = AreaName;

                    }
                }
            }
            catch
            {
                ed1.WriteMessage("\nerror in SM");
            }
            ed1.WriteMessage("\nActif Zone : {0}", Areas.ActiveZone);
            HighlightPoly(Areas.ActiveZone);
        }

        //highlight polyline
        public static void HighlightPoly(string area)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            TypedValue[] filListA = new TypedValue[1]
                {
                    new TypedValue((int)DxfCode.ExtendedDataAsciiString,area)
                };
            TypedValue[] filListB = new TypedValue[1]
                {
                    new TypedValue((int)DxfCode.ExtendedDataRegAppName,"Areas")
                };
            SelectionFilter filterA = new SelectionFilter(filListA);
            PromptSelectionResult perA = ed.SelectAll(filterA);

            SelectionFilter filterB = new SelectionFilter(filListB);
            PromptSelectionResult perB = ed.SelectAll(filterB);

            if (perB.Status == PromptStatus.OK)
            {
                using (Transaction Tx = db.TransactionManager.StartTransaction())
                {
                    foreach (SelectedObject o in perB.Value)
                    {
                        Polyline pline = Tx.GetObject(o.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead) as Polyline;
                        pline.Unhighlight();
                    }
                    Tx.Commit();
                }
            }
            if (perA.Status == PromptStatus.OK)
            {
                using (Transaction Tx = db.TransactionManager.StartTransaction())
                {

                    foreach (SelectedObject o in perA.Value)
                    {
                        Polyline pline = Tx.GetObject(o.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead) as Polyline;
                        pline.Highlight();
                    }
                    Tx.Commit();
                }

            }

        }
    }
}

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost