コード例 #1
0
        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();
                }
            }
        }
コード例 #2
0
            private void CalculatePolyline()
            {
                // Строим временный отрезок из точки 1 в точку 2
                var tmpLine = new Line(_fPoint, _sPoint);

                // Строим три вспомогательных вектора
                var vecCurrentToFirst  = _currPoint - _fPoint;
                var vecCurrentToSecond = _currPoint - _sPoint;
                var vecSecondToFirst   = _sPoint - _fPoint;

                /* Определим катет в треугольнике, которой образуется текущей точкой и второй точкой
                 * через угол между векторами
                 */
                var katet = Math.Sin(vecCurrentToSecond.GetAngleTo(vecSecondToFirst)) * vecCurrentToSecond.Length;

                // Найдем угол между вектором из текущей точки к первой точке и вспомогательной линией
                var angleOnToTmpLinePlane = vecCurrentToFirst.GetAngleTo(vecSecondToFirst, tmpLine.Normal);

                // Получим знак (направление) в зависимости от угла (изменим знак переменной "катет")
                if (angleOnToTmpLinePlane < Math.PI)
                {
                    katet = -katet;
                }

                // Получим 3 точку по направлению и катету и вектор
                var thPoint          = _sPoint + (vecSecondToFirst.GetPerpendicularVector().GetNormal() * katet);
                var vecThirdToSecond = thPoint - _sPoint;

                // Получим 4 точку по тому-же принципу. Для откладывания длины использовать абс.значение!
                Point3d fourPoint;

                if (angleOnToTmpLinePlane < Math.PI)
                {
                    fourPoint = thPoint - (vecThirdToSecond.GetPerpendicularVector().GetNormal() * tmpLine.Length);
                }
                else
                {
                    fourPoint = thPoint + (vecThirdToSecond.GetPerpendicularVector().GetNormal() * tmpLine.Length);
                }

                _polyline.Reset(true, 4);
                _polyline.AddVertexAt(0, new Point2d(_fPoint.X, _fPoint.Y), 0.0, 0.0, 0.0);
                _polyline.AddVertexAt(1, new Point2d(_sPoint.X, _sPoint.Y), 0.0, 0.0, 0.0);
                _polyline.AddVertexAt(2, new Point2d(thPoint.X, thPoint.Y), 0.0, 0.0, 0.0);
                _polyline.AddVertexAt(3, new Point2d(fourPoint.X, fourPoint.Y), 0.0, 0.0, 0.0);
                _polyline.SetDatabaseDefaults();
                _polyline.Closed = true;
            }
コード例 #3
0
ファイル: Commands.cs プロジェクト: vildar82/TileContour
        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();
                }
            });
        }
コード例 #4
0
        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();
                }
            }
        }
コード例 #5
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;
        }
コード例 #6
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);
        }
コード例 #7
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();
            }
        }