Exemple #1
0
        public static string GetLayerName(Autodesk.AutoCAD.DynamoNodes.Document document, SettingsObjectLayerType objectType, string objectName = "")
        {
            CivilDocument        civilDoc        = Autodesk.Civil.ApplicationServices.CivilDocument.GetCivilDocument(document.AcDocument.Database);
            SettingsDrawing      settingsDrawing = civilDoc.Settings.DrawingSettings;
            SettingsObjectLayers sOL             = settingsDrawing.ObjectLayerSettings;
            SettingsObjectLayer  sOLayer         = sOL.GetObjectLayerSetting(objectType);
            string layerName = "";
            string layerMod  = sOLayer.ModifierValue;

            if (sOLayer.ModifierValue.Contains("*"))
            {
                layerMod = sOLayer.ModifierValue.Replace("*", objectName);
            }
            switch (sOLayer.Modifier)
            {
            case (ObjectLayerModifierType.Prefix):
            {
                layerName = layerMod + sOLayer.LayerName;
                break;
            }

            case (ObjectLayerModifierType.Suffix):
            {
                layerName = sOLayer.LayerName + layerMod;
                break;
            }

            default:
            {
                layerName = sOLayer.LayerName;
                break;
            }
            }
            return(layerName);
        }
        /// <summary>
        /// Get document's materials with their names and ObjectId
        /// </summary>
        /// <returns></returns>
        public static Dictionary <string, ObjectId> GetMaterials(Autodesk.AutoCAD.DynamoNodes.Document doc_dyn)
        {
            //Document doc = Application.DocumentManager.MdiActiveDocument;
            Document doc = doc_dyn.AcDocument;
            Database db  = doc.Database;
            Dictionary <string, ObjectId> MaterialAndName = new Dictionary <string, ObjectId>();

            //using (DocumentLock acDocLock = doc.LockDocument())
            //{

            //}
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary MaterialsLib = (DBDictionary)tr.GetObject(db.MaterialDictionaryId, OpenMode.ForRead);
                foreach (DBDictionaryEntry entry in MaterialsLib)
                {
                    Material OneMaterial = tr.GetObject((ObjectId)entry.Value, OpenMode.ForRead) as Material;
                    if (!MaterialAndName.ContainsKey(OneMaterial.Name))
                    {
                        MaterialAndName.Add(OneMaterial.Name, OneMaterial.ObjectId);
                    }
                }
                tr.Commit();
            }
            return(MaterialAndName);
        }
        /// <summary>
        /// Create an AutoCAD's MLeader's object by two points and text's string
        /// </summary>
        /// <param name="place_point"></param>
        /// <param name="leader_line_start"></param>
        /// <param name="leader_text"></param>
        /// <param name="text_width"></param>
        /// <param name="text_height"></param>
        public static void CreateMLeaderByPoint(Autodesk.AutoCAD.DynamoNodes.Document doc_dyn, ds.Point place_point, ds.Point leader_line_start, string leader_text, double TextRotation = 0d, double LandingGap = 0.04, double text_width = 5, double text_height = 0.2, double arrow_size = 0.5)
        {
            /* Help docs
             * http://bushman-andrey.blogspot.com/2013/01/blog-post.html
             * https://adn-cis.org/kak-sozdat-multivyinosku-v-.net.html
             * https://adn-cis.org/forum/index.php?topic=10503.msg49118#msg49118
             */
            Document doc = doc_dyn.AcDocument;
            //Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor   ed = doc.Editor;

            using (Transaction Tx = db.TransactionManager.StartTransaction())
            {
                BlockTable table = Tx.GetObject(
                    db.BlockTableId,
                    OpenMode.ForRead)
                                   as BlockTable;

                BlockTableRecord model = Tx.GetObject(
                    table[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite)
                                         as BlockTableRecord;

                MLeader leader      = new MLeader();
                var     temp_leader = leader.AddLeader();
                //leader.SetArrowSize(0, arrow_size);
                leader.ArrowSize = arrow_size;
                leader.AddLeaderLine(temp_leader);
                leader.AddFirstVertex(temp_leader, new Point3d(place_point.X, place_point.Y, 0.0));
                leader.AddLastVertex(temp_leader, new Point3d(leader_line_start.X, leader_line_start.Y, 0.0));

                leader.SetDatabaseDefaults();

                leader.ContentType = ContentType.MTextContent;
                leader.SetTextAttachmentType(
                    Autodesk.AutoCAD.DatabaseServices.TextAttachmentType.AttachmentBottomLine,
                    Autodesk.AutoCAD.DatabaseServices.LeaderDirectionType.LeftLeader);

                MText mText = new MText();
                mText.SetDatabaseDefaults();
                mText.Width  = text_width;
                mText.Height = text_height;

                mText.SetContentsRtf(leader_text);
                mText.Rotation       = TextRotation;
                leader.MText         = mText;
                leader.LandingGap    = LandingGap;
                mText.BackgroundFill = false;

                model.AppendEntity(leader);
                Tx.AddNewlyCreatedDBObject(leader, true);

                Tx.Commit();
            }
        }
        public static Profile ByAlignmentAndSurface(Autodesk.AutoCAD.DynamoNodes.Document document, Alignment parentAlignment, Surface parentSurface, string name, Styles.ProfileStyle profileStyle, ProfileLabelSet profileLabelSet)
        {
            Autodesk.Civil.DatabaseServices.Profile newProfile = null;
            AcadApp.Document curDoc = document.AcDocument;
            Database         db     = curDoc.Database;

            Autodesk.Civil.DatabaseServices.Surface civilSurface = (Autodesk.Civil.DatabaseServices.Surface)parentSurface.InternalDBObject;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                string oName = name;
                // need to assign these values based on the string inputs
                LayerTable lyrTable = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
                //ObjectId layerId = Utilities.GetLayerByName(db, layer.Name); // id of layer to add profile to
                // get the layer name defined in the settings
                ObjectId           layerId       = Utilities.GetLayerByName(db, CivilDynamoTools.Settings.Settings.GetLayerName(document, Autodesk.Civil.Settings.SettingsObjectLayerType.Profile, name));
                ObjectId           styleId       = profileStyle.InternalObjectId;    // id of style to assign the profile
                ObjectId           labelSetId    = profileLabelSet.InternalObjectId; // id of label set to assign
                CivilDocument      civilDocument = Autodesk.Civil.ApplicationServices.CivilDocument.GetCivilDocument(document.AcDocument.Database);
                ObjectIdCollection alignIds      = civilDocument.GetAlignmentIds();
                // make sure the name doesn't already exist for any profile
                int copy = 1;
                foreach (ObjectId aId in alignIds)
                {
                    Autodesk.Civil.DatabaseServices.Alignment civilAlignment = (Autodesk.Civil.DatabaseServices.Alignment)trans.GetObject(aId, OpenMode.ForRead);
                    ObjectIdCollection profIds = civilAlignment.GetProfileIds();
                    foreach (ObjectId pId in profIds)
                    {
                        Autodesk.Civil.DatabaseServices.Profile civilProfile = (Autodesk.Civil.DatabaseServices.Profile)trans.GetObject(pId, OpenMode.ForRead);
                        if (civilProfile.Name == name)
                        {
                            name  = oName + " - " + copy;
                            copy += 1;
                        }
                    }
                }
                ObjectId newProfileId = Autodesk.Civil.DatabaseServices.Profile.CreateFromSurface(name, parentAlignment.InternalObjectId, parentSurface.InternalObjectId, layerId, styleId, labelSetId);
                newProfile = (Autodesk.Civil.DatabaseServices.Profile)trans.GetObject(newProfileId, OpenMode.ForRead);
                trans.Commit();
            }
            if (newProfile == null)
            {
                return(null);
            }
            else
            {
                return(new Profile(newProfile, true));
            }
        }
        public static List <Surface> GetSurfaces(Autodesk.AutoCAD.DynamoNodes.Document document)
        {
            List <Surface> surfaces = new List <Surface>();

            Autodesk.AutoCAD.ApplicationServices.Document acDoc = document.AcDocument;
            Database           db            = acDoc.Database;
            CivilDocument      civilDocument = CivilDocument.GetCivilDocument(db);
            ObjectIdCollection surfaceIds    = civilDocument.GetSurfaceIds();

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                foreach (ObjectId surfaceId in surfaceIds)
                {
                    surfaces.Add(new Surface((Autodesk.Civil.DatabaseServices.Entity)trans.GetObject(surfaceId, OpenMode.ForRead), false));
                }
            }
            return(surfaces);
        }
        /// <summary>
        /// Getting new ObjectId collection with objects which length are satisfy "needing_length"
        /// </summary>
        /// <param name="objects_id">List with ObjectId</param>
        /// <param name="needing_length">if 1 value -> search all line's that length is equal current;
        /// if 2 value -> search all line's that length is more first and less second; if 3 and more values -> search all line's that length is equal one of current list</param>
        /// <returns>List with ObjectId</returns>
        public static List <ObjectId> GetLinesByLength(Autodesk.AutoCAD.DynamoNodes.Document doc_dyn, List <ObjectId> objects_id, List <double> needing_length, int Accuracy = 8)
        {
            Document doc = doc_dyn.AcDocument;

            for (int i1 = 0; i1 < needing_length.Count; i1++)
            {
                double OneCheckeNum = needing_length[i1];
                needing_length[i1] = Math.Round(OneCheckeNum, Accuracy);
            }
            //Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            List <Autodesk.AutoCAD.DynamoNodes.Object> AcadObjects = new List <Autodesk.AutoCAD.DynamoNodes.Object>();
            List <ObjectId> selected_objects = new List <ObjectId>();

            using (DocumentLock acDocLock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    foreach (ObjectId line_id in objects_id)
                    {
                        Autodesk.AutoCAD.DatabaseServices.Line OneObject = tr.GetObject(line_id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Line;
                        //if (LineType == 1) OneObject = OneObject as Polyline;
                        //else if (LineType == 2) OneObject = OneObject as Arc;
                        //else OneObject = OneObject as Line;
                        double LineLen = Math.Round(OneObject.Length, Accuracy);
                        //Autodesk.AutoCAD.DatabaseServices.Line OneObject = tr.GetObject(line_id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Line;
                        if (needing_length.Count == 1 && LineLen == needing_length[0])
                        {
                            selected_objects.Add(line_id);
                        }
                        else if (needing_length.Count == 2 && LineLen >= needing_length[0] && LineLen <= needing_length[1])
                        {
                            selected_objects.Add(line_id);
                        }
                        else if (needing_length.Contains(LineLen))
                        {
                            selected_objects.Add(line_id);
                        }
                    }
                    tr.Commit();
                }
            }
            return(selected_objects);
        }
        /// <summary>
        /// Get Object's handle as string by it's ObjectId
        /// </summary>
        /// <param name="doc_dyn"></param>
        /// <param name="objects_id"></param>
        /// <returns></returns>
        public static List <string> GetHandlesByObjectsId(Autodesk.AutoCAD.DynamoNodes.Document doc_dyn, List <ObjectId> objects_id)
        {
            Document doc = doc_dyn.AcDocument;
            //Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor        ed = doc.Editor;
            Database      db = doc.Database;
            List <string> HandlesByObjectsId = new List <string>();

            using (DocumentLock acDocLock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    foreach (ObjectId id in objects_id)
                    {
                        //long obj_handle = id.Handle;
                        HandlesByObjectsId.Add($"{id.Handle}");
                    }
                    tr.Commit();
                }
            }
            return(HandlesByObjectsId);
        }
        /// <summary>
        /// Get list with AutoCAD's internal ObjectId for input object collection (their's handle)
        /// </summary>
        /// <param name="acad_objects_list">Input AutoCAD's objects</param>
        /// <returns>List witj ObjectId for objects</returns>]
        public static List <ObjectId> GetObjectIdsByObjects_AcadObj(Autodesk.AutoCAD.DynamoNodes.Document doc_dyn, List <Autodesk.AutoCAD.DynamoNodes.Object> acad_objects_list)
        {
            Document doc = doc_dyn.AcDocument;
            //Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor          ed = doc.Editor;
            Database        db = doc.Database;
            List <ObjectId> ObjectIdsByObjects_AcadObjs = new List <ObjectId>();

            using (DocumentLock acDocLock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    foreach (Autodesk.AutoCAD.DynamoNodes.Object obj_instance in acad_objects_list)
                    {
                        long     obj_handle = Convert.ToInt64(obj_instance.Handle, 16);
                        ObjectId id         = db.GetObjectId(false, new Handle(obj_handle), 0);
                        ObjectIdsByObjects_AcadObjs.Add(id);
                    }
                    tr.Commit();
                }
            }
            return(ObjectIdsByObjects_AcadObjs);
        }
        /// <summary>
        /// DANGEROUS!!!. Can create a FATAL or internal error. Get Autodesk.AutoCAD.DatabaseServices.DBObject by ObjectId
        /// </summary>
        /// <param name="objects_id">List with ObjectId</param>
        /// <param name="mode">OpenMode (read or write) -- actions after returning objects</param>
        /// <returns>Autodesk.AutoCAD.DatabaseServices.DBObject list</returns>
        public static List <Autodesk.AutoCAD.DynamoNodes.Object> GetAcadObjectsByObjectsId(Autodesk.AutoCAD.DynamoNodes.Document doc_dyn, List <ObjectId> objects_id, OpenMode mode)
        {
            Document doc = doc_dyn.AcDocument;
            //Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            List <Autodesk.AutoCAD.DynamoNodes.Object> AcadObjects = new List <Autodesk.AutoCAD.DynamoNodes.Object>();
            List <string> handle_list = new List <string>();

            using (DocumentLock acDocLock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    foreach (ObjectId line_id in objects_id)
                    {
                        Autodesk.AutoCAD.DatabaseServices.DBObject OneObject = tr.GetObject(line_id, mode);
                        Handle obj_handle = OneObject.Handle;
                        handle_list.Add(obj_handle.ToString());
                    }
                    tr.Commit();
                }
            }
            foreach (string OneObjHandle in handle_list)
            {
                AcadObjects.Add(dyn.SelectionByQuery.GetObjectByObjectHandle(OneObjHandle));
            }
            return(AcadObjects);
        }
        public static Dictionary <string, object> GetLineIdBetweenLines(Autodesk.AutoCAD.DynamoNodes.Document doc_dyn, ds_g.Point pnt, List <ObjectId> all_objects, List <ObjectId> arrow_lines, double SearchRadius = 0.01)
        {
            //Document doc = Application.DocumentManager.MdiActiveDocument;
            Document doc      = doc_dyn.AcDocument;
            Database db       = doc.Database;
            Point3d  pnt_acad = new Point3d(pnt.X, pnt.Y, 0.0);

            List <ObjectId> lines_need_check = new List <ObjectId>();
            ObjectId        need_object      = ObjectId.Null;

            ds_g.Point EndPoint = null;
            foreach (ObjectId OneLine in all_objects)
            {
                if (!arrow_lines.Contains(OneLine))
                {
                    lines_need_check.Add(OneLine);
                }
            }
            if (arrow_lines.Count == 2 && all_objects.Count > 2)
            {
                using (DocumentLock acDocLock = doc.LockDocument())
                {
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        Autodesk.AutoCAD.DatabaseServices.Line arrow1 = tr.GetObject(arrow_lines[0], OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Line;
                        Autodesk.AutoCAD.DatabaseServices.Line arrow2 = tr.GetObject(arrow_lines[1], OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Line;
                        foreach (ObjectId line_id in lines_need_check)
                        {
                            Autodesk.AutoCAD.DatabaseServices.Line OneObject = tr.GetObject(line_id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Line;
                            Point3d line_sp = OneObject.StartPoint; Point3d line_ep = OneObject.EndPoint;
                            double  LineLen = OneObject.Length;

                            Point3d p1 = new Point3d(line_sp.X + SearchRadius / LineLen * (line_ep.X - line_sp.X), line_sp.Y + SearchRadius / LineLen * (line_ep.Y - line_sp.Y), 0.0);
                            Point3d p2 = new Point3d(line_ep.X + SearchRadius / LineLen * (line_sp.X - line_ep.X), line_ep.Y + SearchRadius / LineLen * (line_sp.Y - line_ep.Y), 0.0);
                            Point3d p_middle = new Point3d((line_sp.X + line_ep.X) / 2.0, (line_sp.Y + line_ep.Y) / 2.0, 0.0);
                            Point3d p_start;

                            if (GetLenByPoints(pnt_acad, p1) < SearchRadius * 2)
                            {
                                p_start  = p1;
                                EndPoint = ds_g.Point.ByCoordinates(line_ep.X, line_ep.Y, 0);
                            }
                            else
                            {
                                p_start  = p2;
                                EndPoint = ds_g.Point.ByCoordinates(line_sp.X, line_sp.Y, 0);
                            }

                            //if ( GetSideOfPointByLine(arrow1.StartPoint, arrow1.EndPoint, p_start) * GetSideOfPointByLine(arrow2.StartPoint, arrow2.EndPoint, p_start) <0 &&
                            //    GetSideOfPointByLine(arrow2.EndPoint, arrow1.EndPoint, p_start) * GetSideOfPointByLine(arrow2.EndPoint, arrow1.EndPoint, p_middle) < 0)
                            if (IsPointIntoTriangle(arrow1.EndPoint, arrow2.EndPoint, pnt_acad, p_start))
                            {
                                need_object = line_id;
                                break;
                            }
                            bool IsPointIntoTriangle(Point3d t1, Point3d t2, Point3d t3, Point3d c)
                            {
                                double ch1 = (t1.X - c.X) * (t2.Y - t1.Y) - (t2.X - t1.X) * (t1.Y - c.Y);
                                double ch2 = (t2.X - c.X) * (t3.Y - t2.Y) - (t3.X - t2.X) * (t2.Y - c.Y);
                                double ch3 = (t3.X - c.X) * (t1.Y - t3.Y) - (t1.X - t3.X) * (t3.Y - c.Y);

                                if ((ch1 >= 0 && ch2 >= 0 && ch3 >= 0) || (ch1 <= 0 && ch2 <= 0 && ch3 <= 0))
                                {
                                    return(true);
                                }
                                else
                                {
                                    return(false);
                                }
                            }
                        }
                        tr.Commit();
                    }
                }
            }

            return(new Dictionary <string, object>
            {
                { "LineId", need_object }, { "LineEndPoint", EndPoint }
            });
        }