예제 #1
0
        public static void Test()
        {
            AcadApplication   AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.22");
            AcadDocument      doc     = AcadApp.ActiveDocument;
            AcadModelSpace    ms      = doc.ModelSpace;
            AcadSelectionSets SelSets = doc.SelectionSets;
            //SelSets.
            AcadSelectionSet sles = SelSets.Add("Ssd");

            sles.SelectOnScreen();
            Console.WriteLine("{0}", sles.Count);

            List <AcadLWPolyline> curList = new List <AcadLWPolyline>();

            foreach (var item in sles)
            {
                try
                {
                    AcadLWPolyline PL = (AcadLWPolyline)item;
                    if (PL.Closed)
                    {
                        curList.Add(PL);
                    }
                }
                catch (Exception)
                {
                    continue;
                }


                // Console.WriteLine("Length={0}", L.Length);
            }

            sles.Delete();
        }
예제 #2
0
파일: Select.cs 프로젝트: Sivolday/MT
        /// <summary> Выбор примитивов на экране </summary>
        /// <param name="autoCADPrimitivesName"> Имя приметива </param>
        /// <returns> Список приметивов </returns>
        public static List <AcadEntity> OnScreen(string autoCADPrimitivesName)
        {
            var acadDocument = DocumentLocator.GetAcadDocument();

            // Установка пространства модели
            acadDocument.ActiveSpace = AcActiveSpace.acModelSpace;

            AcadSelectionSet selectionSet = acadDocument.SelectionSets.Add(DateTime.Now.Millisecond.ToString());

            Int16[]  filterCode  = new Int16[] { 0 };
            object[] filterValue = new object[] { autoCADPrimitivesName };

            selectionSet.SelectOnScreen(filterCode, filterValue);
            var listOfEntities = new List <AcadEntity>();

            Debug.WriteLine($"Из AutoCADa заполучено ${selectionSet.Count} объектов");

            foreach (AcadEntity entity in selectionSet)
            {
                listOfEntities.Add(entity);
            }

            selectionSet.Delete();
            return(listOfEntities);
        }
        /// <summary>
        /// Agregar nueva selección al documento actual.
        /// </summary>
        /// <param name="SelectionS">Selección a agregar.</param>
        public static object AddSelection(string SelectionS)
        {
            AcadSelectionSet Selection = null;

            if (AcadDoc != null)
            {
                DeleteESelec(SelectionS);
                AcadDoc.PurgeAll();

                Selection = AcadDoc.SelectionSets.Add(SelectionS);
                Selection.SelectOnScreen();
            }
            return(Selection);
        }
예제 #4
0
        public static void GetSelection(out AcadSelectionSet curSelection)
        {
            AcadApplication   AcadApp = null;
            AcadDocument      doc     = null;
            AcadSelectionSets SelSets = null;

            curSelection = null;

            try
            {
                Console.WriteLine("正在连接CAD2018...成功");
                AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.22");
                doc     = AcadApp.ActiveDocument;
                SelSets = doc.SelectionSets;
                Console.WriteLine("选择内容...");
            }
            catch (Exception)
            {
                Console.WriteLine("无法连接AutoCAD...");
                return;
            }


            try
            {
                curSelection = SelSets.Add("NewSelection");
            }
            catch (Exception)
            {
                var item = from AcadSelectionSet a in SelSets where a.Name == "NewSelection" select a;
                curSelection = item.First();
            }
            finally
            {
                curSelection.Clear();
            }


            curSelection.SelectOnScreen();


            Console.WriteLine("共选择{0}个对象。", curSelection.Count);
        }
        // 通过选择初始化一般截面
        public CommonSection(AcadSelectionSet curSelection)
        {
            OuterLine  = null;
            Rebars     = new List <Circle2D>();
            PreRebars  = new List <Circle2D>();
            InnerLines = new List <Polygon2D>();
            Xstrip     = new List <double[]>();
            Ystrip     = new List <double[]>();
            xs         = new List <double>();
            ys         = new List <double>();
            Axs        = new List <double>();
            Ays        = new List <double>();

            var bars = from AcadEntity a in curSelection where a.EntityType == 8 select a as AcadCircle;

            foreach (AcadCircle item in bars)
            {
                if ((int)item.color == 6)
                {
                    PreRebars.Add(item.Circle2D());
                }
                else
                {
                    Rebars.Add(item.Circle2D());
                }
            }
            var tmp = from AcadEntity a in curSelection where a.EntityType == 24 select a as AcadLWPolyline;
            List <AcadLWPolyline> pl2ds = tmp.ToList();

            pl2ds     = pl2ds.OrderByDescending(u => u.Area).ToList();
            OuterLine = pl2ds[0].Polygen2D();
            pl2ds.Remove(pl2ds[0]);
            InnerLines = (from AcadLWPolyline a in pl2ds select a.Polygen2D()).ToList();
            //Xstrip = GetStrip(ref OuterLine, ref InnerLines, true);
            //Ystrip = GetStrip(ref OuterLine, ref InnerLines, false);
            //Axs = (from a in Xstrip select a[1]).ToList();
            //Ays = (from a in Ystrip select a[1]).ToList();
            //xs = (from a in Xstrip select a[0]).ToList();
            //ys = (from a in Ystrip select a[0]).ToList();
            GenStrip();
        }
예제 #6
0
        static public LineLoop SearchLineLoop(AcadSelectionSet acadSelSet)
        {
            LineLoop        ret      = null;
            List <AcadLine> allLines = new List <AcadLine>();

            foreach (var item in acadSelSet)
            {
                try
                {
                    AcadLine L = (AcadLine)item;
                    allLines.Add(L);
                }
                catch (Exception)
                {
                    continue;
                }
            }



            return(ret);
        }
예제 #7
0
        public static void Test()
        {
            AcadApplication   AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.19");
            AcadDocument      doc     = AcadApp.ActiveDocument;
            AcadModelSpace    ms      = doc.ModelSpace;
            AcadSelectionSets SelSets = doc.SelectionSets;


            //SelSets.
            Console.WriteLine("请Tony选择中心线.");
            AcadSelectionSet cl = SelSets.Add(new Random().Next(1000, 9999).ToString());

            cl.Clear();
            cl.SelectOnScreen();
            Console.WriteLine("中心线选择完成.");


            Console.WriteLine("请Tony选择预应力筋.");
            AcadSelectionSet preBars = SelSets.Add(new Random().Next(1000, 9999).ToString());

            preBars.Clear();
            preBars.SelectOnScreen();
            Console.WriteLine("预应力钢筋选择完成,共计{0}根.", preBars.Count);

            Console.WriteLine("请Tony选择方向线.");
            AcadSelectionSet dirL = SelSets.Add(new Random().Next(1000, 9999).ToString());

            dirL.Clear();
            dirL.SelectOnScreen();
            List <AcadLine> NewDirLSet = new List <AcadLine>();

            foreach (var item in dirL)
            {
                AcadLine DirLine = (AcadLine)item;
                NewDirLSet.Add(DirLine);
            }
            NewDirLSet.Sort((x, y) => x.GetMidX().CompareTo(y.GetMidX()));

            Console.WriteLine("预应力钢筋选择完成,共计{0}根.", dirL.Count);

            AcadLWPolyline CLine = (AcadLWPolyline)cl.Item(0);


            FileStream   fs = new FileStream("D:\\TonyReadMe.csv", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);


            foreach (var bar in preBars)
            {
                sw.Flush();
                sw.Write("\n{0},", bar.ToString());
                //Console.WriteLine(bar.ToString());
                AcadLWPolyline PL = (AcadLWPolyline)bar;

                foreach (var item in NewDirLSet)
                {
                    double[] p1, p2;
                    double   dist;
                    try
                    {
                        AcadLine DirLine = item;

                        p1 = DirLine.IntersectWith(CLine, AcExtendOption.acExtendThisEntity);
                        p2 = DirLine.IntersectWith(PL, AcExtendOption.acExtendThisEntity);
                        if (p1.Count() == 0 || p2.Count() == 0)
                        {
                            continue;
                        }
                        dist = Math.Pow((p1[0] - p2[0]), 2) + Math.Pow((p1[1] - p2[1]), 2) + Math.Pow((p1[2] - p2[2]), 2);
                        dist = Math.Sqrt(dist);

                        int fx = p1[1] < p2[1] ? 1 : -1;

                        //Console.Write("{0:F0},",dist);

                        sw.Write("{0:F0},", fx * dist * 1000.0);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }


                // Console.WriteLine("Length={0}", L.Length);
            }


            cl.Delete();
            preBars.Delete();
            dirL.Delete();

            sw.Close();
            fs.Close();
        }
        public void GetSection()
        {
            AcadApplication   AcadApp      = null;
            AcadDocument      doc          = null;
            AcadSelectionSets SelSets      = null;
            AcadSelectionSet  curSelection = null;

            InnerLines.Clear();
            try
            {
#if R22
                Console.WriteLine("正在连接CAD2018...成功");
                AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.22");
#elif R20
                Console.WriteLine("正在连接CAD2016...成功");
                AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.20");
#elif R19
                Console.WriteLine("正在连接CAD2014...成功");
                AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.19");
#elif R18
                Console.WriteLine("正在连接CAD2010...成功");
                AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.18");
#endif


                doc     = AcadApp.ActiveDocument;
                SelSets = doc.SelectionSets;
                Console.WriteLine("选择截面...");
            }
            catch (Exception)
            {
                Console.WriteLine("无法连接AutoCAD...");
                return;
            }


            try
            {
                curSelection = SelSets.Add("GetPLs");
            }
            catch (Exception)
            {
                var item = from AcadSelectionSet a in SelSets where a.Name == "GetPLs" select a;
                curSelection = item.First();
            }
            finally
            {
                curSelection.Clear();
            }


            curSelection.SelectOnScreen();


            Console.WriteLine("共选择{0}个对象。", curSelection.Count);
            Globals.LogBox.AppendText(string.Format("共选择{0}个对象。", curSelection.Count));

            var    PLs   = from AcadEntity a in curSelection where a.EntityType == 24 select a as AcadLWPolyline;
            var    Acs   = from AcadLWPolyline a in PLs select a.Area;
            double AcMax = Acs.Max();

            foreach (AcadLWPolyline item in PLs)
            {
                if (item.Area == AcMax)
                {
                    OuterLine = item.Polygen2D();
                }
                else
                {
                    InnerLines.Add(item.Polygen2D());
                }
            }

            var bars = from AcadEntity a in curSelection where a.EntityType == 8 select a as AcadCircle;
            foreach (AcadCircle item in bars)
            {
                ComRebars.Add(item.Circle2D());
            }


            Console.WriteLine("共拾取1个外边界,{0}个内边界,{1}根钢筋...", InnerLines.Count, ComRebars.Count);

            Xstrip = GetStrip(ref OuterLine, ref InnerLines, true);
            Ystrip = GetStrip(ref OuterLine, ref InnerLines, false);
            xs     = (from a in Xstrip select a[0]).ToList();
            ys     = (from a in Ystrip select a[0]).ToList();
            dx     = xs[1] - xs[0];
            dy     = ys.ElementAt(1) - ys.ElementAt(0);
            Axs    = (from a in Xstrip select a[1] * dx).ToList();
            Ays    = (from a in Ystrip select a[1] * dy).ToList();
        }
 SectionLines(AcadSelectionSet curSel)
 {
 }
예제 #10
0
        private void DWGLoad()
        {
            AcadApplication     acadApp             = new AcadApplicationClass();
            IGeometryCollection pGeometryCollection = new PolygonClass();

            foreach (DataGridViewRow eRow in dataGridView1.Rows)
            {
                if (eRow.Cells[0].Value != null && (bool)eRow.Cells[0].Value == true)
                {
                    if (eRow.Cells["FileSuffix"].Value.ToString().ToLower() == "dwg")
                    {
                        object       o           = Type.Missing;
                        string       strFileName = eRow.Cells["FilePath"].Value.ToString();
                        AcadDocument acadDoc     = acadApp.Documents.Open(strFileName, true, null);
                        System.Windows.Forms.Application.DoEvents();
                        AcadLayer        acadLyer    = acadDoc.Layers.Item(1);
                        AcadSelectionSet ssetObj     = acadDoc.SelectionSets.Add("FWX");
                        short[]          vFilterType = null;
                        object[]         vFilterData = null;
                        vFilterType    = new short[1];
                        vFilterType[0] = 8;
                        vFilterData    = new object[1];
                        vFilterData[0] = "FWX";
                        //ISegmentCollection pSegmentCollection = new RingClass();
                        //pSegmentCollection.AddSegment()
                        ssetObj.Select(AcSelect.acSelectionSetAll, null, null, vFilterType, vFilterData);
                        foreach (AcadObject eEntity in ssetObj)
                        {
                            if (eEntity.ObjectName == "AcDbPolyline")
                            {
                                AcadLWPolyline pPline = (AcadLWPolyline)eEntity;
                                double[]       polyLinePoint;
                                polyLinePoint = (Double[])pPline.Coordinates;
                                int i, pointCount = polyLinePoint.Length / 2;
                                IPointCollection pPointColl = new RingClass();
                                for (i = 0; i < polyLinePoint.Length - 1; i = i + 2)
                                {
                                    IPoint pPoint = new PointClass();
                                    pPoint.X = polyLinePoint[i];
                                    pPoint.Y = polyLinePoint[i + 1];
                                    pPointColl.AddPoint(pPoint, ref o, ref o);
                                }
                                pGeometryCollection.AddGeometry(pPointColl as IRing, ref o, ref o);
                            }
                        }
                    }
                    else if (eRow.Cells["FileSuffix"].Value.ToString().ToLower() == "txt")
                    {
                        object o           = Type.Missing;
                        string strFileName = eRow.Cells["FilePath"].Value.ToString();
                        m_strPointArray.Clear();
                        StreamReader ReadFile = new StreamReader(strFileName, System.Text.Encoding.Default);
                        while (!ReadFile.EndOfStream)
                        {
                            m_strPointArray.Add(ReadFile.ReadLine());
                        }
                        ReadFile.Close();
                        IPointCollection pPointColl = new RingClass();
                        for (int i = 0; i < m_strPointArray.Count; i++)
                        {
                            if (m_strPointArray[i].StartsWith("J"))
                            {
                                string[] split  = m_strPointArray[i].Split(new Char[] { ',', ',' });
                                IPoint   pPoint = new PointClass();
                                pPoint.X = Convert.ToDouble(split[2]);
                                pPoint.Y = Convert.ToDouble(split[3]);
                                pPointColl.AddPoint(pPoint, ref o, ref o);
                            }
                        }
                        pGeometryCollection.AddGeometry(pPointColl as IRing, ref o, ref o);
                    }
                }
            }
            acadApp.Quit();
            //DWGLoaded(this.m_Popfrm, new EventArgs());//引发完成事件,有异常待研究
            System.Windows.Forms.Application.DoEvents();
            if (pGeometryCollection.GeometryCount > 0)
            {
                m_pFeature       = EngineFuntions.m_Layer_BusStation.FeatureClass.CreateFeature();
                m_pFeature.Shape = pGeometryCollection as IPolygon;
                IFields fields = m_pFeature.Fields;
                int     nIndex = fields.FindField("任务号");
                m_pFeature.set_Value(nIndex, m_strFolder.Substring(m_strFolder.LastIndexOf("\\") + 1));
                m_pFeature.Store();
                EngineFuntions.ZoomTo(m_pFeature.ShapeCopy);
            }
            else
            {
                m_pFeature = null;
            }
        }