private ADS.DBObject CreateRectangle(Point3d first, Point3d second)
 {
     ADS.Polyline pl = new ADS.Polyline(4);
     pl.Color = this.graphicsColor;
     double firstX = first.X;
     double secondX = second.X;
     double firstY = first.Y;
     double secondY = second.Y;
     pl.AddVertexAt(0, new Point2d(firstX, firstY), 0, 0, 0);
     pl.AddVertexAt(1, new Point2d(firstX, secondY), 0, 0, 0);
     pl.AddVertexAt(2, new Point2d(secondX, secondY), 0, 0, 0);
     pl.AddVertexAt(3, new Point2d(secondX, firstY), 0, 0, 0);
     pl.Closed = true;
     return pl;
 }
        public static void TestConvexHull()
        {
            var currentDoc = Application.DocumentManager.MdiActiveDocument;
            var editor     = currentDoc.Editor;
            var database   = currentDoc.Database;
            // Only select polyline and polyline 2d.
            ObjectId polylineId = ObjectId.Null;

            while (true)
            {
                var options = new PromptEntityOptions("\n选择一条曲线:");
                var result  = editor.GetEntity(options);
                if (result.Status == PromptStatus.OK)
                {
                    polylineId = result.ObjectId;
                    break;
                }

                if (result.Status == PromptStatus.Cancel)
                {
                    break;
                }

                editor.WriteMessage("\n选择无效");
            }

            if (polylineId.IsNull)
            {
                return;
            }

            using (var transaction = database.TransactionManager.StartTransaction())
            {
                IEnumerable <Point3d> points = CurveUtils.GetDistinctVertices(polylineId, transaction);
                var convex = new ConvexHull <Point3d>(points, it => it.X, it => it.Y,
                                                      (x, y) => new Point3d(x, y, 0), (a, b) => a == b);
                convex.CalcConvexHull();

                var hullPoints = convex.GetResultsAsArrayOfPoint();
                var polyline   = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                for (int i = 0; i < hullPoints.Length; i++)
                {
                    polyline.AddVertexAt(i, new Point2d(hullPoints[i].X, hullPoints[i].Y), 0, 1, 1);
                }
                polyline.ColorIndex = 2;

                var modelspaceId = SymbolUtilityServices.GetBlockModelSpaceId(database);
                var modelspace   = transaction.GetObject(modelspaceId, OpenMode.ForWrite) as BlockTableRecord;
                modelspace.AppendEntity(polyline);
                transaction.AddNewlyCreatedDBObject(polyline, true);
                transaction.Commit();
            }
        }
        protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
        {
            WorldGeometry geo = draw.Geometry;

            if (geo != null)
            {
                Point3dCollection tempPts = new Point3dCollection();
                foreach (Point3d pt in mAllVertexes)
                {
                    tempPts.Add(pt);
                }

                tempPts.Add(mLastVertex);
                if (tempPts.Count > 0)
                {
                    try
                    {
                        Database db = HostApplicationServices.WorkingDatabase;
                        Autodesk.AutoCAD.DatabaseServices.Polyline line = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                        for (int i = 0; i < tempPts.Count; i++)
                        {
                            Point3d pt3d = tempPts[i];
                            Point2d pt2d = new Point2d(pt3d.X, pt3d.Y);
                            line.AddVertexAt(i, pt2d, 0, db.Plinewid, db.Plinewid);
                        }
                        Point2d final = new Point2d(tempPts[0].X, tempPts[0].Y);
                        line.AddVertexAt(tempPts.Count, final, 0, db.Plinewid, db.Plinewid);
                        line.ConstantWidth = HeatSourceLayoutApp.globalProperty.BuildingOutlineWidth;
                        line.Color         = Color.FromRgb(255, 0, 0);
                        draw.Geometry.Draw(line);
                    }
                    catch (System.Exception e)
                    {
                        Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(e.Message);
                    }
                }
            }
            return(true);
        }
        private static Entity DrawPolyline(Point[] points, bool closePart)
        {
            var polyline = new Autodesk.AutoCAD.DatabaseServices.Polyline();
            int num      = 0;

            for (int i = 0; i < points.Length; i++)
            {
                Point  point = points[i];
                PointN srcPt = (PointN)point;
                polyline.AddVertexAt(num++, GIS2CAD.ToCadPoint2d(srcPt), 0.0, 0.0, 0.0);
            }
            return(polyline);
        }
Beispiel #5
0
        public static Autodesk.AutoCAD.DatabaseServices.Polyline CreatePolyline(List <Point2d> points, VisualOption opt)
        {
            Point2d[] pts = DistincPoints(points);
            var       pl  = new Autodesk.AutoCAD.DatabaseServices.Polyline();

            for (int i = 0; i < pts.Length; i++)
            {
                pl.AddVertexAt(i, pts[i], 0, 0, 0);
            }
            pl.Closed = true;
            SetEntityOpt(pl, opt);
            return(pl);
        }
Beispiel #6
0
        private AcadDB.Polyline GetPolylineFromBulgeVertexCollection(BulgeVertexCollection bulges)
        {
            var    polyline   = new AcadDB.Polyline(bulges.Count);
            double totalBulge = 0;

            for (int i = 0; i < bulges.Count; i++)
            {
                BulgeVertex bulgeVertex = bulges[i];
                polyline.AddVertexAt(i, bulgeVertex.Vertex, bulgeVertex.Bulge, 1.0, 1.0);
                totalBulge += bulgeVertex.Bulge;
            }
            polyline.Closed = bulges[0].Vertex.IsEqualTo(bulges[bulges.Count - 1].Vertex) ? true : false;
            return(polyline);
        }
        public IEnumerable <Entity> CreateCrossMark(Point3d position)
        {
            var markSize = ErrorMarkSettings.CurrentSettings.MarkerSize;
            var result   = new List <Entity>();

            var vector1   = new Vector3d(1, 0, 0);
            var vector2   = new Vector3d(0, 1, 0);
            var radius    = markSize / 2.0;
            var point1    = position + vector1 * radius;
            var point2    = position - vector1 * radius;
            var point3    = position + vector2 * radius;
            var point4    = position - vector2 * radius;
            var polyline1 = new Autodesk.AutoCAD.DatabaseServices.Polyline();
            var polyline2 = new Autodesk.AutoCAD.DatabaseServices.Polyline();

            polyline1.AddVertexAt(0, new Point2d(point1.X, point1.Y), 0, 0, 0);
            polyline1.AddVertexAt(1, new Point2d(point2.X, point2.Y), 0, 0, 0);
            polyline2.AddVertexAt(0, new Point2d(point3.X, point3.Y), 0, 0, 0);
            polyline2.AddVertexAt(1, new Point2d(point4.X, point4.Y), 0, 0, 0);
            result.Add(polyline1);
            result.Add(polyline2);
            return(result);
        }
 public static Autodesk.AutoCAD.DatabaseServices.Polyline ConvertPolyCurveToPolyline(Autodesk.DesignScript.Geometry.PolyCurve polyCurve)
 {
     Autodesk.AutoCAD.DatabaseServices.Polyline retVal = new Autodesk.AutoCAD.DatabaseServices.Polyline();
     if (polyCurve != null)
     {
         // extract each segment
         Autodesk.DesignScript.Geometry.Curve[] curves = new Autodesk.DesignScript.Geometry.Curve[polyCurve.NumberOfCurves];
         // convert segment into AutoCAD polyline
         int curIndex = 0;
         while (curIndex < polyCurve.NumberOfCurves)
         {
             double bulge = 0.0;
             Autodesk.DesignScript.Geometry.Curve curCurve = polyCurve.CurveAtIndex(curIndex);
             // trying to figure out if the segment is an arc or line...for some reason it thinks lines are arcs
             var typeOfcurve = curCurve.GetType();
             if (typeOfcurve == typeof(Autodesk.DesignScript.Geometry.Arc))
             {
                 // set the bulge for the curve
                 //bulge = BulgeFromCurve(curCurve, true);
                 bulge = 0.5;
             }
             // otherwise, it's a line, and default bulge applies
             retVal.AddVertexAt(curIndex, new Point2d(curCurve.StartPoint.X, curCurve.StartPoint.Y), bulge, 0.0, 0.0);
             curIndex = curIndex + 1;
         }
         if (polyCurve.StartPoint == polyCurve.EndPoint)
         {
             retVal.Closed = true;
         }
         else
         {
             retVal.AddVertexAt(curIndex, new Point2d(polyCurve.EndPoint.X, polyCurve.EndPoint.Y), 0.0, 0.0, 0.0);
         }
         //retVal = Autodesk.DesignScript.Geometry.PolyCurve.ByJoinedCurves(curves);
     }
     return(retVal);
 }
Beispiel #9
0
        public static void darwBounds()
        {
            Document doc      = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database database = doc.Database;

            using (Transaction trans = database.TransactionManager.StartTransaction())
            {
                BlockTable       black_table       = trans.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord blocktable_record = trans.GetObject(black_table[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                using (Autodesk.AutoCAD.DatabaseServices.Polyline poly = new Autodesk.AutoCAD.DatabaseServices.Polyline())
                {
                    poly.AddVertexAt(0, new Point2d(-m_earth_radius, -m_earth_radius), 0, 10, 10);
                    poly.AddVertexAt(1, new Point2d(m_earth_radius, -m_earth_radius), 0, 10, 10);
                    poly.AddVertexAt(2, new Point2d(m_earth_radius, m_earth_radius), 0, 10, 10);
                    poly.AddVertexAt(3, new Point2d(-m_earth_radius, m_earth_radius), 0, 10, 10);
                    poly.AddVertexAt(4, new Point2d(-m_earth_radius, -m_earth_radius), 0, 10, 10);
                    poly.Closed = true;
                    poly.Color  = Autodesk.AutoCAD.Colors.Color.FromRgb(255, 0, 0);
                    blocktable_record.AppendEntity(poly);
                    trans.AddNewlyCreatedDBObject(poly, true);
                }
                trans.Commit();
            }
        }
Beispiel #10
0
        public static void TileContour()
        {
            AcadLib.CommandStart.Start((doc) =>
            {
                Database db = doc.Database;
                Editor ed = doc.Editor;

                // Jig отрисовки полилинии по задаваемым точкам.
                TileLineJig jigTest = new TileLineJig();
                PromptResult jigRes;
                bool status = true;
                do
                {
                    jigRes = ed.Drag(jigTest);
                    if (jigRes.Status == PromptStatus.OK)
                    {
                        // Добавление указанной точки
                        jigTest.AddNewPromptPoint();
                    }
                    else if (jigRes.Status == PromptStatus.Cancel || jigRes.Status == PromptStatus.Error)
                    {
                        return;
                    }
                    else if (jigRes.Status == PromptStatus.Other)
                    {
                        status = false;
                    }
                } while (status);

                // Добавление полилинии в чертеж.
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    Autodesk.AutoCAD.DatabaseServices.Polyline pl = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                    pl.SetDatabaseDefaults();
                    for (int i = 0; i < jigTest.AllVertex.Count; i++)
                    {
                        Point3d pt3d = jigTest.AllVertex[i];
                        Point2d pt2d = new Point2d(pt3d.X, pt3d.Y);
                        pl.AddVertexAt(i, pt2d, 0, db.Plinewid, db.Plinewid);
                    }
                    pl.TransformBy(jigTest.UCS);
                    btr.AppendEntity(pl);
                    tr.AddNewlyCreatedDBObject(pl, true);
                    tr.Commit();
                }
            });
        }
Beispiel #11
0
        /// <summary>
        /// 绘制多段线
        /// </summary>
        /// <param name="pts">二维点的集合</param>
        /// <param name="width">绘制线段的宽度</param>
        /// <returns>多段线对象</returns>
        public static Polyline DrawPolyline(Point2dCollection pts, double width)
        {
            try
            {
                Polyline ent = new Polyline();
                for (int i = 0; i < pts.Count; i++)
                {
                    ent.AddVertexAt(i, pts[i], 0, width, width);
                }


                return(ent);
            }
            catch
            {
                return(new Polyline());
            }
        }
        [IsVisibleInDynamoLibrary(false)]         // make this true in order to test this function
        public static CivilDynamoTools.Objects.AutoCAD.Polyline BaseCurve2d(Parcel parcel)
        {
            //This will fail to be set if there are any 3d segments
            Autodesk.AutoCAD.DatabaseServices.Polyline poly = parcel.BaseCurve as Autodesk.AutoCAD.DatabaseServices.Polyline;
            if (poly != null)
            {
                // return poly;
                return(new CivilDynamoTools.Objects.AutoCAD.Polyline(poly));
            }

            //3d segments found, loop through and create 2d vertices for new polyline
            poly = new Autodesk.AutoCAD.DatabaseServices.Polyline();
            object comparcel = parcel.AcadObject;

            object[] args = new object[1];
            args[0] = 0;

            object loop = (object)comparcel.GetType().InvokeMember("ParcelLoops", System.Reflection.BindingFlags.GetProperty, null, comparcel, args);

            int count = (int)loop.GetType().InvokeMember("Count", System.Reflection.BindingFlags.GetProperty, null, loop, null);

            for (int i = 0; i < count; i++)
            {
                args[0] = i;
                object segelement = (object)loop.GetType().InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, loop, args);
                double x          = (double)segelement.GetType().InvokeMember("StartX", System.Reflection.BindingFlags.GetProperty, null, segelement, null);
                double y          = (double)segelement.GetType().InvokeMember("StartY", System.Reflection.BindingFlags.GetProperty, null, segelement, null);
                double bulge      = 0;
                try
                {
                    bulge = (double)segelement.GetType().InvokeMember("Bulge", System.Reflection.BindingFlags.GetProperty, null, segelement, null);
                }
                catch { }
                poly.AddVertexAt(i, new Point2d(x, y), bulge, 0, 0);
            }
            poly.Closed = true;
            //return poly;
            return(new CivilDynamoTools.Objects.AutoCAD.Polyline(poly));
        }
Beispiel #13
0
        protected override bool WorldDraw(WorldDraw draw)
        {
            //draw.Geometry.Draw(myline);
            for (int i = 0; i < pts.Count; i++)
            {
                if (mypl.NumberOfVertices > i)
                {
                    mypl.SetPointAt(i, pts[i]);
                }
                else
                {
                    mypl.AddVertexAt(i, pts[i], 0, 0, 0);
                }
            }
            while (pts.Count < mypl.NumberOfVertices)
            {
                mypl.RemoveVertexAt(pts.Count - 1);
            }

            draw.Geometry.Draw(mypl);
            return(true);
        }
Beispiel #14
0
        public Polyline CopyPolyline(BlockTableRecord spaceRecord, ref Polyline pline, Transaction tr)
        {
            Polyline newpLine = new Polyline();
            int      max      = pline.NumberOfVertices;

            for (int i = 0; i < max; i++)
            {
                newpLine.AddVertexAt(i, pline.GetPoint2dAt(i), pline.GetBulgeAt(i), pline.GetStartWidthAt(i), pline.GetEndWidthAt(i));
            }

            newpLine.Closed = pline.Closed;
            //newpLine.Layer = pline.Layer.Split('|').Last();
            newpLine.Layer      = frameBlockAboveLayer;
            newpLine.Normal     = pline.Normal;
            newpLine.Color      = pline.Color;
            newpLine.ColorIndex = pline.ColorIndex;

            spaceRecord.UpgradeOpen();
            spaceRecord.AppendEntity(newpLine);
            tr.AddNewlyCreatedDBObject(newpLine, true);
            spaceRecord.DowngradeOpen();

            return(newpLine);
        }
Beispiel #15
0
        public void draw_rec()
        {
            // Start a transaction
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Open the Block table for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;
                // Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;

                // Create a polyline with two segments (3 points)
                Autodesk.AutoCAD.DatabaseServices.Polyline acPoly1 = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                Autodesk.AutoCAD.DatabaseServices.Polyline acPoly2 = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                Autodesk.AutoCAD.DatabaseServices.Polyline acPoly3 = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                Autodesk.AutoCAD.DatabaseServices.Polyline acPoly4 = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                Autodesk.AutoCAD.DatabaseServices.Polyline acPoly5 = new Autodesk.AutoCAD.DatabaseServices.Polyline();

                // rec1
                acPoly1.SetDatabaseDefaults();
                acPoly1.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
                acPoly1.AddVertexAt(1, new Point2d(100, 0), 0, 0, 0);
                acPoly1.AddVertexAt(2, new Point2d(100, 200), 0, 0, 0);
                acPoly1.AddVertexAt(3, new Point2d(0, 200), 0, 0, 0);
                acPoly1.AddVertexAt(4, new Point2d(0, 0), 0, 0, 0);

                // rec2
                acPoly2.SetDatabaseDefaults();
                acPoly2.AddVertexAt(0, new Point2d(200, 0), 0, 0, 0);
                acPoly2.AddVertexAt(1, new Point2d(450, 0), 0, 0, 0);
                acPoly2.AddVertexAt(2, new Point2d(450, 500), 0, 0, 0);
                acPoly2.AddVertexAt(3, new Point2d(200, 500), 0, 0, 0);
                acPoly2.AddVertexAt(4, new Point2d(200, 0), 0, 0, 0);

                // rec3
                acPoly3.SetDatabaseDefaults();
                acPoly3.AddVertexAt(0, new Point2d(550, 0), 0, 0, 0);
                acPoly3.AddVertexAt(1, new Point2d(700, 0), 0, 0, 0);
                acPoly3.AddVertexAt(2, new Point2d(700, 250), 0, 0, 0);
                acPoly3.AddVertexAt(3, new Point2d(550, 250), 0, 0, 0);
                acPoly3.AddVertexAt(4, new Point2d(550, 0), 0, 0, 0);

                // rec4
                acPoly4.SetDatabaseDefaults();
                acPoly4.AddVertexAt(0, new Point2d(800, 0), 0, 0, 0);
                acPoly4.AddVertexAt(1, new Point2d(1150, 0), 0, 0, 0);
                acPoly4.AddVertexAt(2, new Point2d(1150, 550), 0, 0, 0);
                acPoly4.AddVertexAt(3, new Point2d(800, 550), 0, 0, 0);
                acPoly4.AddVertexAt(4, new Point2d(800, 0), 0, 0, 0);

                // rec5
                acPoly5.SetDatabaseDefaults();
                acPoly5.AddVertexAt(0, new Point2d(1250, 0), 0, 0, 0);
                acPoly5.AddVertexAt(1, new Point2d(1500, 0), 0, 0, 0);
                acPoly5.AddVertexAt(2, new Point2d(1500, 550), 0, 0, 0);
                acPoly5.AddVertexAt(3, new Point2d(1250, 550), 0, 0, 0);
                acPoly5.AddVertexAt(4, new Point2d(1250, 0), 0, 0, 0);


                // Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(acPoly1);
                acBlkTblRec.AppendEntity(acPoly2);
                acBlkTblRec.AppendEntity(acPoly3);
                acBlkTblRec.AppendEntity(acPoly4);
                acBlkTblRec.AppendEntity(acPoly5);
                acTrans.AddNewlyCreatedDBObject(acPoly1, true);
                acTrans.AddNewlyCreatedDBObject(acPoly2, true);
                acTrans.AddNewlyCreatedDBObject(acPoly3, true);
                acTrans.AddNewlyCreatedDBObject(acPoly4, true);
                acTrans.AddNewlyCreatedDBObject(acPoly5, true);
                // Save the new object to the database
                acTrans.Commit();
            }
        }
Beispiel #16
0
        /// <summary>
        /// 创建布局和视口
        ///
        /// </summary>
        public static void CreateLayout(double[] B_Sta, Document doc, Database db)
        {
            //  var doc = Application.DocumentManager.MdiActiveDocument;

            if (doc == null)
            {
                return;
            }
            // var db = doc.Database;
            var ed     = doc.Editor;
            var ext    = new Extents2d();
            int k      = 0;// 100000 / 2600 / 3
            int zhushi = 0;
            //求布局数
            double Layoutnum = (B_Sta[B_Sta.Length - 1] - B_Sta[0]) / 350 / 3;

            for (int j = 0; j < Layoutnum + 1; j++)
            {
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    //以读模式打开块表
                    BlockTable acBlkTbl;
                    acBlkTbl = tr.GetObject(db.BlockTableId,
                                            OpenMode.ForRead) as BlockTable;
                    //以写模式打开块表记录Paper空间
                    BlockTableRecord acBlkTblRec;
                    acBlkTblRec = tr.GetObject(acBlkTbl[BlockTableRecord.PaperSpace],
                                               OpenMode.ForWrite) as BlockTableRecord;
                    //切换到Paper空间布局
                    Application.SetSystemVariable("TILEMODE", 0);
                    doc.Editor.SwitchToPaperSpace();

                    // Create and select a new layout tab
                    var id = LayoutManager.Current.CreateAndMakeLayoutCurrent("病害处理" + j);
                    // Open the created layout
                    var lay = (Layout)tr.GetObject(id, OpenMode.ForWrite);
                    // Make some settings on the layout and get its extents

                    /*     lay.SetPlotSettings(
                     *   //  "ISO_full_bleed_2A0_(1189.00_x_1682.00_MM)", // Try this big boy!
                     *     "ANSI_B_(11.00_x_17.00_Inches)",
                     *     "monochrome.ctb",
                     *     "DWF6 ePlot.pc3");*/
                    //从布局中获取PlotInfo
                    PlotInfo acPlInfo = new PlotInfo();
                    acPlInfo.Layout = lay.ObjectId;
                    //复制布局中的PlotSettings
                    PlotSettings acPlSet = new PlotSettings(lay.ModelType);
                    acPlSet.CopyFrom(lay);
                    //更新PlotSettings对象
                    PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
                    //设置打印区域
                    acPlSetVdr.SetPlotType(acPlSet,
                                           Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
                    //设置打印比例
                    acPlSetVdr.SetUseStandardScale(acPlSet, true);
                    acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);
                    //居中打印
                    acPlSetVdr.SetPlotCentered(acPlSet, true);
                    ext = lay.GetMaximumExtents();
                    //绘制边框
                    //Create a polyline with two segments (3 points)
                    using (Autodesk.AutoCAD.DatabaseServices.Polyline acPoly = new Autodesk.AutoCAD.DatabaseServices.Polyline())

                    {
                        acPoly.AddVertexAt(0, new Point2d(0.2, 0.7), 0, 0, 0);
                        acPoly.AddVertexAt(1, new Point2d(0.2, ext.MaxPoint.Y - 0.3), 0, 0, 0);
                        acPoly.AddVertexAt(2, new Point2d(ext.MaxPoint.X - 0.13, ext.MaxPoint.Y - 0.3), 0, 0, 0);
                        acPoly.AddVertexAt(3, new Point2d(ext.MaxPoint.X - 0.13, 0.7), 0, 0, 0);
                        acPoly.AddVertexAt(4, new Point2d(0.2, 0.7), 0, 0, 0);
                        acPoly.LineWeight = LineWeight.LineWeight200;
                        //将新对象添加到块表记录和事务
                        acBlkTblRec.AppendEntity(acPoly);
                        tr.AddNewlyCreatedDBObject(acPoly, true);
                    }
                    //创建下方直线分隔
                    double[] Xiafangzuobiao = new double[] { 1.5, 4, 7, 7.5, 8.1, 8.6, 9.2, 9.7, 10.3, 10.8 };
                    for (int i = 0; i < Xiafangzuobiao.Length; i++)
                    {
                        WxxTU.Createline(acBlkTblRec, tr, Xiafangzuobiao[i], Xiafangzuobiao[i], 0.7, 1);
                    }
                    //创建下方需要填写的文字
                    string[] Xiafangwenzi        = new string[] { "路面病害处治设计图", "设计", "复核", "审核", "图号" };
                    double[] Xiafangwenzizuobiao = new double[] { 4.5, 7, 8.15, 9.2, 10.4 };
                    for (int i = 0; i < Xiafangwenzi.Length; i++)
                    {
                        //创建一个单行文字对象
                        using (DBText acText = new DBText())
                        {
                            acText.Position = new Point3d(Xiafangwenzizuobiao[i], 0.8, 0);
                            acText.Height   = 0.15;

                            acText.TextString = Xiafangwenzi[i];
                            acText.ColorIndex = 0;
                            //文字反向   acText.IsMirroredInX = false;
                            acText.Rotation = 0;
                            acBlkTblRec.AppendEntity(acText);
                            tr.AddNewlyCreatedDBObject(acText, true);


                            //释放DBObject对象
                        }
                    }
                    //创建上方直线分割
                    double[] Shangfangzuobiao = new double[] { 10, 10.8 };
                    for (int i = 0; i < Shangfangzuobiao.Length; i++)
                    {
                        WxxTU.Createline(acBlkTblRec, tr, Shangfangzuobiao[i], Shangfangzuobiao[i], ext.MaxPoint.Y - 0.3, ext.MaxPoint.Y - 0.65);
                    }
                    //创建上方需要填写的文字
                    string[] Shangfangwenzi = new string[] { "第" + j + "页", "共" + Math.Truncate(Layoutnum) + "页" };
                    for (int i = 0; i < Shangfangwenzi.Length; i++)
                    {
                        //创建一个单行文字对象
                        using (DBText acText = new DBText())
                        {
                            acText.Position = new Point3d(Shangfangzuobiao[i], ext.MaxPoint.Y - 0.5, 0);
                            acText.Height   = 0.15;

                            acText.TextString = Shangfangwenzi[i];
                            acText.ColorIndex = 0;
                            //文字反向   acText.IsMirroredInX = false;
                            acText.Rotation = 0;
                            acBlkTblRec.AppendEntity(acText);
                            tr.AddNewlyCreatedDBObject(acText, true);


                            //释放DBObject对象
                        }
                    }
                    //创建桩号起终点注释
                    double[] Sta_Y = new double[] { 2.75, 5, 7.1 };
                    for (int i = 0; i < 3; i++)
                    {
                        using (DBText acText = new DBText())
                        {
                            acText.Position = new Point3d(9, Sta_Y[i], 0);
                            acText.Height   = 0.15;

                            acText.TextString = WxxTU.Station_Double2Str(B_Sta[0] + 350 * (zhushi - 3)) + "-" + WxxTU.Station_Double2Str(B_Sta[0] + 350 * (zhushi - 2));
                            acText.ColorIndex = 0;
                            //文字反向   acText.IsMirroredInX = false;
                            acText.Rotation = 0;
                            acBlkTblRec.AppendEntity(acText);
                            tr.AddNewlyCreatedDBObject(acText, true);
                            zhushi++;

                            //释放DBObject对象
                        }
                    }

                    //创建多个视口
                    for (int i = 0; i < 3; i++)
                    {
                        lay.ApplyToViewport(
                            tr, i,
                            vp =>
                        {
                            // Size the viewport according to the extents calculated when
                            // we set the PlotSettings (device, page size, etc.)
                            // Use the standard 10% margin around the viewport
                            // (found by measuring pixels on screenshots of Layout1, etc.)

                            vp.ResizeViewport(ext, 0.8, 2 * i + 1, k);
                            k++;

                            /* // Adjust the view so that the model contents fit
                             * if (ValidDbExtents(db.Extmin, db.Extmax))
                             * {
                             *   vp.FitContentToViewport(new Extents3d(db.Extmin, db.Extmax),0.8);
                             * }
                             *
                             * // Finally we lock the view to prevent meddling*/


                            vp.Locked = true;
                        }
                            );
                    }
                    //  k++;

                    // Commit the transaction
                    tr.Commit();
                }
            }
            // Zoom so that we can see our new layout, again with a little padding

            /*    ed.Command("_.ZOOM", "_E");
             *  ed.Command("_.ZOOM", ".7X");
             *  ed.Regen();*/
        }
Beispiel #17
0
        /// <summary>
        /// 添加矩形面域
        /// </summary>
        /// <param name="color"></param>
        /// <param name="B_x"></param>
        /// <param name="B_y"></param>
        /// <param name="width"></param>
        /// <param name="length"></param>
        /// <param name="acDoc"></param>
        /// <param name="acCurDb"></param>
        public static void AddQuYu(int ColorIndex, double B_x, double B_y, double width, double length, string Filltype, Document acDoc, Database acCurDb)
        {
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读打开Block表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;
                //以写方式打开Block表的Model空间记录
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;
                //在Model空间创建四边形实体(矩形)

                //Create a polyline with two segments (3 points)
                using (Autodesk.AutoCAD.DatabaseServices.Polyline acPoly = new Autodesk.AutoCAD.DatabaseServices.Polyline())

                {
                    acPoly.AddVertexAt(0, new Point2d(B_x, B_y), 0, 0, 0);
                    acPoly.AddVertexAt(1, new Point2d(B_x + length, B_y), 0, 0, 0);
                    acPoly.AddVertexAt(2, new Point2d(B_x + length, B_y - width), 0, 0, 0);
                    acPoly.AddVertexAt(3, new Point2d(B_x, B_y - width), 0, 0, 0);
                    acPoly.AddVertexAt(4, new Point2d(B_x, B_y), 0, 0, 0);
                    //将新对象添加到块表记录并登记事务
                    acBlkTblRec.AppendEntity(acPoly);
                    acPoly.ColorIndex = ColorIndex;

                    acTrans.AddNewlyCreatedDBObject(acPoly, true);


                    // Adds the ac2DSolidBow to an object id array
                    ObjectIdCollection acObjIdColl = new ObjectIdCollection();
                    acObjIdColl.Add(acPoly.ObjectId);
                    AddExtDict(acPoly);

                    //-------------------------------
                    // 创建填充对象
                    //-------------------------------
                    // Create the hatch object and append it to the block table record
                    using (Hatch acHatch = new Hatch())
                    {
                        acBlkTblRec.AppendEntity(acHatch);
                        acTrans.AddNewlyCreatedDBObject(acHatch, true);

                        // Set the properties of the hatch object
                        // Associative must be set after the hatch object is appended to the
                        // block table record and before AppendLoop
                        acHatch.PatternScale = 6;
                        acHatch.SetHatchPattern(HatchPatternType.PreDefined, Filltype);

                        acHatch.Associative = true;
                        acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl);
                        acHatch.EvaluateHatch(true);
                        acHatch.ColorIndex = ColorIndex;
                        AddExtDict(acHatch);
                    }

                    //释放DBObject对象
                }

                //提交事务,将新对象保存到数据库
                acTrans.Commit();
            }
        }
Beispiel #18
0
        /// <summary>
        /// 添加线
        /// </summary>
        /// <param name="Chuliqujian"></param>
        /// <param name="B_Sta"></param>
        /// <param name="line_width"></param>
        public static void AddLine(double Chuliqujian, double[] B_Sta, double[] line_width, Document acDoc, Database acCurDb)
        {
            Application.SetSystemVariable("CECOLOR", " 0");

            //获取当前文档及数据库
            //  Document acDoc = Application.DocumentManager.MdiActiveDocument;
            //  Database acCurDb = acDoc.Database;
            //绘制横线
            #region
            for (int j = 0; j < line_width.Length; j++)
            {
                //启动事务
                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    //以读模式打开块表
                    BlockTable acBlkTbl;
                    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                                 OpenMode.ForRead) as BlockTable;
                    //以写模式打开 Block 表记录 Model 空间
                    BlockTableRecord acBlkTblRec;
                    acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                    OpenMode.ForWrite) as BlockTableRecord;
                    //Create a polyline with two segments (3 points)
                    using (Autodesk.AutoCAD.DatabaseServices.Polyline acPoly = new Autodesk.AutoCAD.DatabaseServices.Polyline())

                    {
                        for (int i = 0; i < B_Sta.Length; i++)

                        {
                            acPoly.AddVertexAt(i, new Point2d(B_Sta[i], line_width[j]), 0, 0, 0);
                        }
                        //将新对象添加到块表记录和事务
                        acBlkTblRec.AppendEntity(acPoly);
                        acTrans.AddNewlyCreatedDBObject(acPoly, true);
                    }
                    //将新对象保存到数据库
                    acTrans.Commit();
                }
            }
            #endregion

            //绘制竖线
            #region
            double Fen = B_Sta[B_Sta.Length - 1] / Chuliqujian;
            for (int i = 0; i < Fen + 1; i++)
            {
                //启动事务
                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    //以读模式打开块表
                    BlockTable acBlkTbl;
                    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                                 OpenMode.ForRead) as BlockTable;
                    //以写模式打开 Block 表记录 Model 空间
                    BlockTableRecord acBlkTblRec;
                    acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                    OpenMode.ForWrite) as BlockTableRecord;
                    //以 5,5、12,3两点为起终点创建一条直线
                    using (Line acLine = new Line(new Point3d(B_Sta[0] + i * Chuliqujian, 0, 0),
                                                  new Point3d(B_Sta[0] + i * Chuliqujian, line_width[line_width.Length - 1], 0)))
                    {
                        //将新对象添加到块表记录和事务
                        acBlkTblRec.AppendEntity(acLine);
                        acTrans.AddNewlyCreatedDBObject(acLine, true);
                        //释放DBObject对象
                    }
                    //将新对象保存到数据库
                    acTrans.Commit();
                }
            }
            #endregion
        }
Beispiel #19
0
        public void Cmd2()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Autodesk.AutoCAD.DatabaseServices.Polyline pyLine = new Autodesk.AutoCAD.DatabaseServices.Polyline();


            var bsPointRes = ed.GetPoint(new PromptPointOptions("\n请输入圆心"));

            if (bsPointRes.Status == PromptStatus.OK)
            {
                pyLine.AddVertexAt(pyLine.NumberOfVertices, new Point2d(bsPointRes.Value.X, bsPointRes.Value.Y), 0, 0, 0);
                var jigPtOpts = new JigPromptPointOptions();

                var dimAlign = new AlignedDimension();

                var donut = new JigHelper();

                donut.SetEntities(new Entity[] { pyLine, dimAlign });

                for (int i = 1; i < int.MaxValue; i++)
                {
                    donut.PrapareForNextInput(jigPtOpts, "\n请输入下一个点");


                    donut.SetUpdate(jig =>
                    {
                        if (pyLine.NumberOfVertices == i)
                        {
                            pyLine.AddVertexAt(pyLine.NumberOfVertices, new Point2d(jig.Point.X, jig.Point.Y), 0, 0, 0);
                        }
                        else
                        {
                            pyLine.SetPointAt(i, new Point2d(jig.Point.X, jig.Point.Y));
                        }

                        Point3d pt1 = pyLine.GetPoint3dAt(i - 1);
                        Point3d pt2 = pyLine.GetPoint3dAt(i);

                        var vec3d  = pt1 - pt2;
                        var vec3d2 = vec3d.RotateBy(Math.PI / 2, Vector3d.ZAxis);



                        Point3d cPoint3d = new Point3d((pt1.X + pt2.X) / 2, (pt1.Y + pt2.Y) / 2, 0);

                        dimAlign.XLine1Point = pt1;
                        dimAlign.XLine2Point = pt2;

                        dimAlign.DimLinePoint = cPoint3d + vec3d2 * 0.5;

                        dimAlign.DimensionText = null;

                        dimAlign.DimensionStyle = ObjectId.Null;
                    });
                    dimAlign.ToSpace();
                    dimAlign = new AlignedDimension();

                    var status = donut.Drag();
                    if (status == PromptStatus.Cancel)
                    {
                        break;
                    }
                    else if (status != PromptStatus.OK)
                    {
                        return;
                    }
                }

                pyLine.ToSpace();
            }
        }
        /// <summary>
        /// 获取指定高程处的等高线。与绘制样式无关(即这个高程可能位于相邻小等高线之间,不绘制,但用此函数无论如何总可以得到这条等高线)。高程超范围抛出ArgumentOutOfRangeException。
        /// </summary>
        /// <param name="elevation">高程</param>
        /// <returns>等高线</returns>
        public Polyline[] GetContourAt(double elevation)
        {
            Interval inter = GetElevationRange();

            if (elevation > inter.UpperBound || elevation < inter.LowerBound)
            {
                throw new ArgumentException("指定高程超出范围");
            }
            // 内插等值点,将有等值点的网格加入到真值表中
            List <Polyline> contours = new List <Polyline>();

            m_truetable = new List <Grid>();
            for (int i = 0; i < m_xLength - 1; i++)
            {
                for (int j = 0; j < m_yLength - 1; j++)
                {
                    int nCount = m_grids[i, j].setContourValue(elevation);
                    if (nCount > 0)
                    {
                        m_truetable.Add(m_grids[i, j]);
                    }
                }
            }

            while (m_truetable.Count > 0)
            {
                // 任取一个网格开始追踪
                Grid    grid  = m_truetable[0];
                Point2d point = grid.getEPoint(0);
                // 记录第一个网格和第一个等值点的位置,用于反向跟踪
                int      firstgrid_x = grid.X;
                int      firstgrid_y = grid.Y;
                int      firstEPPos  = grid.getEPointPos(0);
                bool     flag        = false;          // 反向标记,如果未进行反向追踪为false,否则为true
                Polyline pline       = new Polyline(); // 待追踪的等高线

                pline.AddVertexAt(pline.NumberOfVertices, tranCoordinate(point), 0, 0, 0);
                while (grid != null && !pline.Closed)
                {
                    int pos;
                    point = grid.findNextEPoint(point, out pos);
                    if (flag)
                    {
                        pline.AddVertexAt(0, tranCoordinate(point), 0, 0, 0);
                    }
                    else
                    {
                        pline.AddVertexAt(pline.NumberOfVertices, tranCoordinate(point), 0, 0, 0);
                    }

                    // 如果该网格无等值点,则将其从真值表中移出
                    if (grid.EPCount == 0)
                    {
                        m_truetable.Remove(grid);
                    }
                    bool boundflag;   // 边界标志,是否碰到边界
                    grid = findnext(grid.X, grid.Y, pos, out boundflag);
                    if (grid == null && boundflag && !flag)
                    {
                        flag = true;
                        // 反向追踪
                        bool firstboundflag;  // 第一个网格上的第一个等值点是否在边界上
                        grid = findnext(firstgrid_x, firstgrid_y, firstEPPos, out firstboundflag);
                        if (firstboundflag)
                        {
                            break;
                        }
                    }
                }
                contours.Add(pline);
            }
            return(contours.ToArray());
        }
        private static Entity DrawPart(Segment[] segs, Point[] points, Point[] densifiedPoints, bool closePart, bool hasZ, double defaultElevation)
        {
            double num = 0.0;

            if (segs != null)
            {
                if (segs.Length == 1)
                {
                    CircularArc        circularArc = segs[0] as CircularArc;
                    EllipticArc        ellipticArc = segs[0] as EllipticArc;
                    BezierCurve        bezierCurve = segs[0] as BezierCurve;
                    ArcGIS10Types.Line line        = segs[0] as ArcGIS10Types.Line;
                    if (circularArc != null)
                    {
                        if (((PointN)circularArc.FromPoint).X == ((PointN)circularArc.ToPoint).X && ((PointN)circularArc.FromPoint).Y == ((PointN)circularArc.ToPoint).Y)
                        {
                            return(GIS2CAD.DrawCircle(circularArc, defaultElevation));
                        }
                        return(GIS2CAD.DrawCircularArc(circularArc, defaultElevation, densifiedPoints));
                    }
                    else if (ellipticArc != null)
                    {
                        if (!ellipticArc.IsCounterClockwise)
                        {
                            return(AGSEllipticalArc.ToCadSpline(ellipticArc, defaultElevation));
                        }
                        return(AGSEllipticalArc.ToCadEllipse(ellipticArc, defaultElevation));
                    }
                    else
                    {
                        if (line != null)
                        {
                            return(GIS2CAD.DrawPolyline(densifiedPoints, false));
                        }
                        if (bezierCurve != null)
                        {
                            return(GIS2CAD.DrawPolyline(densifiedPoints, closePart));
                        }
                    }
                }
                else if (segs.Length > 1)
                {
                    PointN pointN = segs[0].FromPoint as PointN;
                    num = pointN.Z;
                    if (num == 0.0)
                    {
                        num = defaultElevation;
                    }
                    if (GIS2CAD.CanBeDrawnAsPolyline(segs))
                    {
                        var polyline = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                        polyline.ColorIndex = (256);
                        int    num2    = 0;
                        PointN pointN2 = (PointN)segs[0].ToPoint;
                        for (int i = 0; i < segs.Length; i++)
                        {
                            Segment     segment      = segs[i];
                            CircularArc circularArc2 = segment as CircularArc;
                            var         line2        = segment as ArcGIS10Types.Line;
                            if (line2 != null)
                            {
                                PointN pointN3 = (PointN)line2.FromPoint;
                                polyline.AddVertexAt(num2++, new Point2d(pointN3.X, pointN3.Y), 0.0, -1.0, -1.0);
                                pointN2 = (PointN)line2.ToPoint;
                            }
                            else if (circularArc2 != null)
                            {
                                PointN pointN4 = (PointN)circularArc2.CenterPoint;
                                PointN pointN5 = (PointN)circularArc2.FromPoint;
                                PointN pointN6 = (PointN)circularArc2.ToPoint;
                                new Point2d(pointN5.X - pointN4.X, pointN5.Y - pointN4.Y);
                                new Point2d(pointN6.X - pointN4.X, pointN6.Y - pointN4.Y);
                                Point2d point2d     = new Point2d(pointN5.X, pointN5.Y);
                                Point2d centerPoint = new Point2d(pointN4.X, pointN4.Y);
                                Point2d point2d2    = new Point2d(pointN6.X, pointN6.Y);
                                double  num3        = Math.Abs(centerPoint.GetDistanceTo(point2d));
                                double  num4        = Math.Abs(point2d.GetDistanceTo(point2d2));
                                double  num5        = num3;
                                double  num6        = num3;
                                double  d           = (num5 * num5 + num6 * num6 - num4 * num4) / (2.0 * num5 * num6);
                                double  num7        = Math.Acos(d);
                                num7 = GIS2CAD.CalcThetaFromVectors(point2d, point2d2, centerPoint, circularArc2.IsCounterClockwise);
                                double num8 = Math.Tan(num7 / 4.0);
                                if (!circularArc2.IsCounterClockwise)
                                {
                                    num8 *= -1.0;
                                }
                                polyline.AddVertexAt(num2++, point2d, num8, -1.0, -1.0);
                                pointN2 = pointN6;
                            }
                        }
                        polyline.AddVertexAt(num2, new Point2d(pointN2.X, pointN2.Y), 0.0, -1.0, -1.0);
                        if (closePart)
                        {
                            polyline.Closed = (true);
                        }
                        return(polyline);
                    }
                    return(GIS2CAD.Draw3dPline(densifiedPoints, closePart));
                }
            }
            else if (points != null)
            {
                if (points.Length == 2)
                {
                    var line3 = new Autodesk.AutoCAD.DatabaseServices.Line();
                    line3.ColorIndex = (256);
                    GIS2CAD.AdjustZ(ref points, defaultElevation);
                    Point3d startPoint = GIS2CAD.ToCadPoint3d((PointN)points[0]);
                    Point3d endPoint   = GIS2CAD.ToCadPoint3d((PointN)points[1]);
                    line3.StartPoint = (startPoint);
                    line3.EndPoint   = (endPoint);
                    return(line3);
                }
                if (points.Length > 0)
                {
                    if (!GIS2CAD.IsPlanar(points))
                    {
                        try
                        {
                            Document document           = AfaDocData.ActiveDocData.Document;
                            var      database           = document.Database;
                            var      transactionManager = document.TransactionManager;
                            using (document.LockDocument())
                            {
                                using (Transaction transaction = transactionManager.StartTransaction())
                                {
                                    BlockTable       blockTable       = (BlockTable)transaction.GetObject(database.BlockTableId, 0);
                                    BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[(BlockTableRecord.ModelSpace)], (OpenMode)1);
                                    Polyline3d       polyline3d       = new Polyline3d();
                                    polyline3d.ColorIndex = (256);
                                    blockTableRecord.AppendEntity(polyline3d);
                                    transaction.AddNewlyCreatedDBObject(polyline3d, true);
                                    Point[] array = points;
                                    for (int j = 0; j < array.Length; j++)
                                    {
                                        PointN           srcPt            = (PointN)array[j];
                                        PolylineVertex3d polylineVertex3d = new PolylineVertex3d(GIS2CAD.ToCadPoint3d(srcPt));
                                        polyline3d.AppendVertex(polylineVertex3d);
                                        transaction.AddNewlyCreatedDBObject(polylineVertex3d, true);
                                    }
                                    if (closePart)
                                    {
                                        polyline3d.Closed = (true);
                                    }
                                    document.TransactionManager.QueueForGraphicsFlush();
                                    document.TransactionManager.FlushGraphics();
                                    document.Editor.UpdateScreen();
                                    transaction.Commit();
                                    return(polyline3d);
                                }
                            }
                        }
                        catch (System.Exception ex)
                        {
                            string arg_526_0 = ex.Message;
                        }
                    }
                    var polyline2 = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                    polyline2.ColorIndex = (256);
                    polyline2.Elevation  = (num);
                    num = ((PointN)points[0]).Z;
                    if (num == 0.0)
                    {
                        num = defaultElevation;
                    }
                    int     num9   = 0;
                    Point[] array2 = points;
                    for (int k = 0; k < array2.Length; k++)
                    {
                        PointN pointN7 = (PointN)array2[k];
                        polyline2.AddVertexAt(num9++, new Point2d(pointN7.X, pointN7.Y), 0.0, -1.0, -1.0);
                    }
                    if (closePart)
                    {
                        polyline2.Closed = (true);
                    }
                    return(polyline2);
                }
            }
            return(null);
        }
Beispiel #22
0
        public static ObjectId CreateExtrusion(Point2d[] pts, Point3d destPt, Vector3d normal, WoodGrainAlignment grain)
        {
            ObjectId entId = ObjectId.Null;

            Document activeDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            Database db        = activeDoc.Database;

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

                BlockTableRecord btr = null;
                if (String.IsNullOrEmpty(blockName))
                {
                    btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                }
                else
                {
                    if (bt.Has(blockName))
                    {
                        btr = tr.GetObject(bt[blockName], OpenMode.ForWrite) as BlockTableRecord;
                    }
                    else
                    {
                        btr      = new BlockTableRecord();
                        btr.Name = blockName;
                        bt.UpgradeOpen();
                        bt.Add(btr);
                        tr.AddNewlyCreatedDBObject(btr, true);
                    }
                }

                Solid3d extrudedSolid = new Solid3d();
                using (Autodesk.AutoCAD.DatabaseServices.Polyline outline = new Autodesk.AutoCAD.DatabaseServices.Polyline())
                {
                    outline.SetDatabaseDefaults();
                    outline.Normal = normal;

                    int cnt = 0;
                    foreach (Point2d pt in pts)
                    {
                        outline.AddVertexAt(cnt, pt, 0, 0, 0);
                        cnt++;
                    }
                    outline.Closed = true;

                    Extents3d exts  = outline.GeometricExtents;
                    Point3d   minPt = exts.MinPoint;
                    Point3d   maxPt = exts.MaxPoint;

                    double p1 = maxPt.X - minPt.X;
                    double p2 = maxPt.Y - minPt.Y;
                    double p3 = maxPt.Z - minPt.Z;

                    double pmin = 0.0;
                    if (p1 == 0)
                    {
                        pmin = Math.Min(p2, p3);
                    }
                    if (p2 == 0)
                    {
                        pmin = Math.Min(p1, p3);
                    }
                    if (p3 == 0)
                    {
                        pmin = Math.Min(p1, p2);
                    }
                    double pmax = Math.Max(Math.Max(p1, p2), p3);

                    extrudedSolid.RecordHistory = true;

                    plyIndex++;

                    Vector3d heightVector = outline.Normal * t;

                    SweepOptions sweepOptions = new SweepOptions();

                    SweepOptionsBuilder builder = new SweepOptionsBuilder(sweepOptions);

                    extrudedSolid.CreateExtrudedSolid(outline, heightVector, sweepOptions);
                }

                entId = btr.AppendEntity(extrudedSolid);
                tr.AddNewlyCreatedDBObject(extrudedSolid, true);

                extrudedSolid.TransformBy(Matrix3d.Displacement(destPt.GetAsVector()));

                tr.Commit();
            }

            return(entId);
        }
        public static ObjectId DrawPolyLine(out bool status)
        {
            status = false;
            using (DocumentLock docLock = Application.DocumentManager.MdiActiveDocument.LockDocument())
            {
                ObjectId res = ObjectId.Null;
                try
                {
                    Database db = HostApplicationServices.WorkingDatabase;
                    jigger = new PolyLineJig();
                    PromptResult jigRes;

                    //最初版本,勿删
                    do
                    {
                        jigRes = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.Drag(jigger);
                        if (jigger.mAllVertexes.Count > 0 && (jigger.mAllVertexes[jigger.mAllVertexes.Count - 1] - jigger.mLastVertex).Length == 0)
                        {
                        }
                        else if (jigRes.Status == PromptStatus.OK)
                        {
                            jigger.mAllVertexes.Add(jigger.mLastVertex);
                        }
                    } while (jigRes.Status == PromptStatus.OK);

                    if (jigRes.Status == PromptStatus.None)
                    {
                        status = true;
                        using (Transaction tr = db.TransactionManager.StartTransaction())
                        {
                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                            Autodesk.AutoCAD.DatabaseServices.Polyline ent = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                            //ent.SetDatabaseDefaults();
                            if (jigger.mAllVertexes.Count > 2)
                            {
                                for (int i = 0; i < jigger.mAllVertexes.Count; i++)
                                {
                                    Point3d pt3d = jigger.mAllVertexes[i];
                                    Point2d pt2d = new Point2d(pt3d.X, pt3d.Y);
                                    ent.AddVertexAt(i, pt2d, 0, db.Plinewid, db.Plinewid);
                                }
                                int     final = jigger.mAllVertexes.Count;
                                Point3d mPt3d = jigger.mAllVertexes[0];
                                Point2d mPt2d = new Point2d(mPt3d.X, mPt3d.Y);
                                ent.AddVertexAt(final, mPt2d, 0, db.Plinewid, db.Plinewid);
                                //ent.TransformBy(jigger.UCS);
                                ent.ConstantWidth = HeatSourceLayoutApp.globalProperty.BuildingOutlineWidth;
                                res = btr.AppendEntity(ent);
                                tr.AddNewlyCreatedDBObject(ent, true);
                                tr.Commit();
                            }
                        }
                    }
                    else
                    {
                        status = false;
                    }
                }
                catch (System.Exception ex)
                {
                    MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
                }
                return(res);
            }
        }
        public static ObjectId CreateExtrusion(Point2d[] pts, Point3d destPt, Vector3d normal, WoodGrainAlignment grain)
        {
            ObjectId entId = ObjectId.Null;

            Document activeDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            Database db = activeDoc.Database;

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

                BlockTableRecord btr = null;
                if (String.IsNullOrEmpty(blockName))
                    btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                else
                {
                    if (bt.Has(blockName))
                        btr = tr.GetObject(bt[blockName], OpenMode.ForWrite) as BlockTableRecord;
                    else
                    {
                        btr = new BlockTableRecord();
                        btr.Name = blockName;
                        bt.UpgradeOpen();
                        bt.Add(btr);
                        tr.AddNewlyCreatedDBObject(btr, true);
                    }
                }

                Solid3d extrudedSolid = new Solid3d();
                using (Autodesk.AutoCAD.DatabaseServices.Polyline outline = new Autodesk.AutoCAD.DatabaseServices.Polyline())
                {
                    outline.SetDatabaseDefaults();
                    outline.Normal = normal;

                    int cnt = 0;
                    foreach (Point2d pt in pts)
                    {
                        outline.AddVertexAt(cnt, pt, 0, 0, 0);
                        cnt++;
                    }
                    outline.Closed = true;

                    Extents3d exts = outline.GeometricExtents;
                    Point3d minPt = exts.MinPoint;
                    Point3d maxPt = exts.MaxPoint;

                    double p1 = maxPt.X - minPt.X;
                    double p2 = maxPt.Y - minPt.Y;
                    double p3 = maxPt.Z - minPt.Z;

                    double pmin = 0.0;
                    if (p1 == 0)
                    {
                        pmin = Math.Min(p2, p3);
                    }
                    if (p2 == 0)
                    {
                        pmin = Math.Min(p1, p3);
                    }
                    if (p3 == 0)
                    {
                        pmin = Math.Min(p1, p2);
                    }
                    double pmax = Math.Max(Math.Max(p1, p2), p3);

                    extrudedSolid.RecordHistory = true;

                    plyIndex++;

                    Vector3d heightVector = outline.Normal * t;

                    SweepOptions sweepOptions = new SweepOptions();

                    SweepOptionsBuilder builder = new SweepOptionsBuilder(sweepOptions);

                    extrudedSolid.CreateExtrudedSolid(outline, heightVector, sweepOptions);
                }

                entId = btr.AppendEntity(extrudedSolid);
                tr.AddNewlyCreatedDBObject(extrudedSolid, true);

                extrudedSolid.TransformBy(Matrix3d.Displacement(destPt.GetAsVector()));

                tr.Commit();
            }

            return entId;
        }
        public static void CreateHandleBlock()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = null;

                if (bt.Has("Handle"))
                    btr = tr.GetObject(bt["Handle"], OpenMode.ForWrite) as BlockTableRecord;
                else
                {
                    btr = new BlockTableRecord();
                    btr.Name = "Handle";
                    bt.UpgradeOpen();
                    bt.Add(btr);
                    tr.AddNewlyCreatedDBObject(btr, true);
                }

                using (Autodesk.AutoCAD.DatabaseServices.Polyline outline = new Autodesk.AutoCAD.DatabaseServices.Polyline())
                {
                    outline.SetDatabaseDefaults();
                    outline.Normal = Vector3d.XAxis;

                    outline.AddVertexAt(0, new Point2d(0.0, 0.0), 0, 0, 0);
                    outline.AddVertexAt(1, new Point2d(0.0, 0.5 * t), 0, 0, 0);
                    outline.AddVertexAt(2, new Point2d(-t, 0.5 * t), 0, 0, 0);
                    outline.AddVertexAt(3, new Point2d(-t, 0.75 * t), 0, 0, 0);
                    outline.AddVertexAt(4, new Point2d(-1.25 * t, t), 0, 0, 0);
                    outline.AddVertexAt(5, new Point2d(-1.75 * t, t), 0, 0, 0);
                    outline.AddVertexAt(6, new Point2d(-2.0 * t, 0.75 * t), 0, 0, 0);
                    outline.AddVertexAt(7, new Point2d(-2.0 * t, 0.0), 0, 0, 0);
                    outline.Closed = true;

                    RevolveOptions opts = new RevolveOptions();
                    RevolveOptionsBuilder rob = new RevolveOptionsBuilder(opts);
                    rob.CloseToAxis = true;
                    rob.DraftAngle = 0;
                    rob.TwistAngle = 0;

                    Solid3d handle = new Solid3d(); ;
                    handle.CreateRevolvedSolid(outline, new Point3d(0, 0, 0), Vector3d.YAxis, 2.0 * Math.PI, 0, rob.ToRevolveOptions());
                    handle.ColorIndex = 0;

                    btr.AppendEntity(handle);
                    tr.AddNewlyCreatedDBObject(handle, true);

                    tr.Commit();
                }
            }
        }
Beispiel #26
0
        public void InputMap()
        {
            string WolrdMapName = "大世界";
            string layer        = "Ground";

            CurrentWorldMap = WolrdMapName;
            CurrentLayer    = layer;
            //line1 = new Line();
            //Point3d point1 = new Point3d(0, 0, 0);
            //Point3d point2 = new Point3d(100, 0, 0);
            //line1.StartPoint = point1;
            //line1.EndPoint = point2;

            //using (Transaction trans = db.TransactionManager.StartTransaction())
            //{

            //    btr.AppendEntity(line1);
            //    trans.AddNewlyCreatedDBObject(line1,true);
            //    trans.Commit();
            //}

            // Get the current database and start a transaction
            Database acCurDb;

            acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Define the name and image to use


                RasterImageDef acRasterDef;
                bool           bRasterDefCreated = false;
                ObjectId       acImgDefId;

                // Get the image dictionary
                ObjectId acImgDctID = RasterImageDef.GetImageDictionary(acCurDb);

                // Check to see if the dictionary does not exist, it not then create it
                if (acImgDctID.IsNull)
                {
                    acImgDctID = RasterImageDef.CreateImageDictionary(acCurDb);
                }

                // Open the image dictionary
                DBDictionary acImgDict = acTrans.GetObject(acImgDctID, OpenMode.ForRead) as DBDictionary;


                ReadMapData();

                //创建世界地图的边缘矩形
                Document         doc = Application.DocumentManager.MdiActiveDocument;
                Database         db  = doc.Database;
                BlockTable       bt  = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = acTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                double worldWidth  = AllMap[CurrentWorldMap].width;
                double worldHeight = AllMap[CurrentWorldMap].height;

                Point2d p1 = new Point2d(0, 0);
                Point2d p2 = new Point2d(worldWidth, 0);
                Point2d p3 = new Point2d(worldWidth, worldHeight);
                Point2d p4 = new Point2d(0, worldHeight);
                Autodesk.AutoCAD.DatabaseServices.Polyline RectPoly = new Autodesk.AutoCAD.DatabaseServices.Polyline();
                RectPoly.AddVertexAt(0, p1, 0, 0, 0);
                RectPoly.AddVertexAt(1, p2, 0, 0, 0);
                RectPoly.AddVertexAt(2, p3, 0, 0, 0);
                RectPoly.AddVertexAt(3, p4, 0, 0, 0);

                RectPoly.Closed = true;
                WorldEdgeRect   = btr.AppendEntity(RectPoly);
                acTrans.AddNewlyCreatedDBObject(RectPoly, true);
                //Line line1 = new Line() { StartPoint = new Point3d(0, 0, 0), EndPoint = new Point3d(worldWidth, 0, 0) };
                //Line line2 = new Line() { StartPoint = new Point3d(0, worldWidth, 0), EndPoint = new Point3d(worldWidth, worldHeight, 0) };
                //Line line3 = new Line() { StartPoint = new Point3d(worldWidth, worldHeight, 0), EndPoint = new Point3d(0, worldHeight, 0) };
                //Line line4 = new Line() { StartPoint = new Point3d(0, worldHeight, 0), EndPoint = new Point3d(0, 0, 0) };
                //btr.AppendEntity(line1);
                //btr.AppendEntity(line2);
                //btr.AppendEntity(line3);
                //btr.AppendEntity(line4);
                //acTrans.AddNewlyCreatedDBObject(line1, true);
                //acTrans.AddNewlyCreatedDBObject(line2, true);
                //acTrans.AddNewlyCreatedDBObject(line3, true);
                //acTrans.AddNewlyCreatedDBObject(line4, true);
                //Entity ent1 = (Entity)line1;
                //Entity ent2 = (Entity)line2;
                //Entity ent3 = (Entity)line3;
                //Entity ent4 = (Entity)line4;
                //Group group = new Group();
                //group.Append(ent1.ObjectId);
                //group.Append(ent2.ObjectId);
                //group.Append(ent3.ObjectId);
                //group.Append(ent4.ObjectId);
                //DBDictionary groupdic = acTrans.GetObject(db.GroupDictionaryId, OpenMode.ForWrite) as DBDictionary;
                //group.Selectable = true;
                //group.Name = "mapEdge";
                //groupdic.SetAt(group.Name, group);
                //acTrans.AddNewlyCreatedDBObject(group, true);
                //~创建世界地图的边缘矩形
                foreach (var iter in AllMap[WolrdMapName].maps[layer])
                {
                    string strImgName  = iter.MapName;
                    string strFileName = mapDir + "\\" + iter.MapName + "\\" + layer + ".png";
                    if (acImgDict.Contains(strImgName))
                    {
                        acImgDefId = acImgDict.GetAt(strImgName);

                        acRasterDef = acTrans.GetObject(acImgDefId, OpenMode.ForWrite) as RasterImageDef;
                    }
                    else
                    {
                        // Create a raster image definition
                        RasterImageDef acRasterDefNew = new RasterImageDef();

                        // Set the source for the image file
                        acRasterDefNew.SourceFileName = strFileName;

                        // Load the image into memory
                        acRasterDefNew.Load();

                        // Add the image definition to the dictionary
                        acImgDict.UpgradeOpen();
                        acImgDefId = acImgDict.SetAt(strImgName, acRasterDefNew);

                        acTrans.AddNewlyCreatedDBObject(acRasterDefNew, true);

                        acRasterDef = acRasterDefNew;

                        bRasterDefCreated = true;
                    }

                    // Open the Block table for read
                    BlockTable acBlkTbl;
                    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

                    // Open the Block table record Model space for write
                    BlockTableRecord acBlkTblRec;
                    acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                    OpenMode.ForWrite) as BlockTableRecord;

                    // Create the new image and assign it the image definition
                    using (RasterImage acRaster = new RasterImage())
                    {
                        acRaster.ImageDefId = acImgDefId;
                        //image = acRaster;
                        // Use ImageWidth and ImageHeight to get the size of the image in pixels (1024 x 768).
                        // Use ResolutionMMPerPixel to determine the number of millimeters in a pixel so you
                        // can convert the size of the drawing into other units or millimeters based on the
                        // drawing units used in the current drawing.

                        // Define the width and height of the image
                        Vector3d width;
                        Vector3d height;

                        // Check to see if the measurement is set to English (Imperial) or Metric units
                        //if (acCurDb.Measurement == MeasurementValue.English)
                        //{
                        //width = new Vector3d((acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth) / 25.4, 0, 0);
                        //height = new Vector3d(0, (acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight) / 25.4, 0);
                        //}
                        //else
                        //{

                        if (iter.width < 0)
                        {
                            iter.width = acRaster.ImageWidth;
                        }
                        if (iter.height < 0)
                        {
                            iter.height = acRaster.ImageHeight;
                        }
                        if (iter.x < 0)
                        {
                            iter.x = 0;
                        }
                        if (iter.y < 0)
                        {
                            iter.y = 0;
                        }
                        width  = new Vector3d(iter.width, 0, 0);
                        height = new Vector3d(0, iter.height, 0);
                        //}

                        // Define the position for the image
                        Point3d insPt = new Point3d(iter.x, iter.y, 0.0);

                        // Define and assign a coordinate system for the image's orientation
                        CoordinateSystem3d coordinateSystem = new CoordinateSystem3d(insPt, width, height);
                        acRaster.Orientation = coordinateSystem;

                        // Set the rotation angle for the image
                        acRaster.Rotation = 0;

                        // Add the new object to the block table record and the transaction
                        iter.ID = acBlkTblRec.AppendEntity(acRaster);
                        acTrans.AddNewlyCreatedDBObject(acRaster, true);

                        // Connect the raster definition and image together so the definition
                        // does not appear as "unreferenced" in the External References palette.
                        RasterImage.EnableReactors(true);
                        acRaster.AssociateRasterDef(acRasterDef);

                        if (bRasterDefCreated)
                        {
                            acRasterDef.Dispose();
                        }
                    }
                }
                // Save the new object to the database
                acTrans.Commit();

                // Dispose of the transaction
            }
        }