示例#1
0
        /// <summary>
        /// 由中心点和半径在UCS中创建球体
        /// </summary>
        /// <param name="cenPt">中心点</param>
        /// <param name="radius">半径</param>
        /// <returns>返回创建的球体的Id</returns>
        public static ObjectId AddSphere(Point3d cenPt, double radius)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor   ed = Application.DocumentManager.MdiActiveDocument.Editor;

            if (radius < 0.00001)
            {
                ed.WriteMessage("\n参数不当,创建球体失败!");
                return(ObjectId.Null);
            }

            // 创建
            Solid3d ent = new Solid3d();

            ent.RecordHistory = true;
            ent.CreateSphere(radius);

            // 位置调整
            Matrix3d mt = ed.CurrentUserCoordinateSystem;

            mt = mt * Matrix3d.Displacement(cenPt - Point3d.Origin);
            ent.TransformBy(mt);

            ObjectId entId = ObjectId.Null;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                entId = db.AddToModelSpace(ent);
                tr.Commit();
            }
            return(entId);
        }
示例#2
0
        // 由中心点和半径创建球体的函数.
        public static ObjectId AddSphere(Point3d cenPt, double radius)
        {
            Solid3d ent = new Solid3d();

            ent.CreateSphere(radius);
            Matrix3d mt = Matrix3d.Displacement(cenPt - Point3d.Origin);

            ent.TransformBy(mt);
            ObjectId entId = AppendEntity(ent);

            return(entId);
        }
示例#3
0
        public void Create3DBox()
        {
            Solid3d bbb = new Solid3d();

            bbb.CreatePyramid(50, 3, 50, 0);
            bbb.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 250, 0);
            ToModelSpace(bbb);

            Solid3d bbbb = new Solid3d();

            bbbb.CreateSphere(50);
            // Move(bbbb, new Point3d(-50, -50, -50));
            ToModelSpace(bbbb);
        }
示例#4
0
        private Solid3d CreateSphere()
        {
            // create the sphere
            Solid3d sphere = new Solid3d();

            sphere.SetDatabaseDefaults();
            // let's create a random number generator for the sphere
            Random randomGenerator = new Random();
            double radius          = randomGenerator.NextDouble() * 50;

            // create the sphere
            sphere.CreateSphere(radius);
            // randomize the position using the randomizer!
            Matrix3d randomMover;
            double   xVec = randomGenerator.NextDouble() * 500;
            double   yVec = randomGenerator.NextDouble() * 400;
            double   zVec = randomGenerator.NextDouble() * 300;

            randomMover = Matrix3d.Displacement(new Vector3d(xVec, yVec, zVec));
            // now apply the the transform to the sphere
            sphere.TransformBy(randomMover);

            // set the material name
            const string matname = "Sitework.Paving - Surfacing.Riverstone.Mortared";

            using (DBDictionary matdict = mPreviewCtrl.mCurrentDwg.MaterialDictionaryId.Open(OpenMode.ForRead) as DBDictionary)
            {
                // if we have found the material, set it to the sphere
                if (matdict.Contains(matname))
                {
                    sphere.Material = matname;
                }
                else
                {
                    // get the editor object
                    Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
                    ed.WriteMessage("\nMaterial (" + matname + ") not found" + " - sphere will be rendered without it.", matname);
                }
            }

            return(sphere);
        }
    protected override bool WorldDrawData(WorldDraw draw)
    {
      if (!base.WorldDrawData(draw))
        return false;

      TransientManager ctm =
        TransientManager.CurrentTransientManager;
      IntegerCollection ints = new IntegerCollection();

      // Draw any outstanding segments (and do so only once)

      bool wasCalibrating = _calibrating;

      while (_lineSegs.Count > 0)
      {
        // Get the line segment and remove it from the list

        LineSegment3d ls = _lineSegs[0];
        _lineSegs.RemoveAt(0);

        // Create an equivalent, red, database line
        // (or yellow, if calibrating)

        Line ln = new Line(ls.StartPoint, ls.EndPoint);
        ln.ColorIndex = (wasCalibrating ? 2 : 1);
        _lines.Add(ln);

        // Draw it as transient graphics

        ctm.AddTransient(
          ln, TransientDrawingMode.DirectShortTerm,
          128, ints
        );

        _calibrating = false;
      }

      if (_drawing)
      {
        if (_cursor == null)
        {
          if (_vertices.Count > 0)
          {
            // Clear our skeleton

            ClearTransients();

            _curPt = _vertices[_vertices.Count - 1];

            // Make our sphere 10cm in diameter (5cm radius)

            Solid3d sol = new Solid3d();
            sol.CreateSphere(0.05);
            _cursor = sol;
            _cursor.TransformBy(
              Matrix3d.Displacement(_curPt - Point3d.Origin)
            );

            _cursor.ColorIndex = 2;

            ctm.AddTransient(
              _cursor, TransientDrawingMode.DirectShortTerm,
              128, ints
            );
          }
        }
        else
        {
          if (_vertices.Count > 0)
          {
            Point3d newPt = _vertices[_vertices.Count - 1];
            _cursor.TransformBy(
              Matrix3d.Displacement(newPt - _curPt)
            );
            _curPt = newPt;

            ctm.UpdateTransient(_cursor, ints);
          }
        }
      }
      else // !_drawing
      {
        if (_cursor != null)
        {
          ctm.EraseTransient(_cursor, ints);
          _cursor.Dispose();
          _cursor = null;
        }
      }

      return true;
    }
    protected override bool WorldDrawData(WorldDraw draw)
    {
      if (!base.WorldDrawData(draw))
        return false;

      short origCol = draw.SubEntityTraits.Color;

      if (_resizing)
      {
        using (Solid3d sphere = new Solid3d())
        {
          try
          {
            sphere.CreateSphere(_profRad);

            if (sphere != null)
            {
              sphere.TransformBy(
                Matrix3d.Displacement(
                  _resizeLocation - Point3d.Origin
                )
              );

              // Draw the cursor

              draw.SubEntityTraits.Color = ColorIndex;
              sphere.WorldDraw(draw);
            }
          }
          catch (System.Exception ex)
          {
            _doc.Editor.WriteMessage(
              "\nException: {0} - {1}", ex.Message, ex.InnerException
            );
          }
          finally
          {
            draw.SubEntityTraits.Color = origCol;
          }
        }
        return true;
      }

      // If we're currently drawing...

      if (_drawing)
      {
        Solid3d sol = null;
        try
        {
          // If we have vertices that haven't yet been drawn...
 
          if (_vertices.Count > 1 //&&
              //_vertices.Count - 1 > _lastDrawnVertex
            )
          {
            // ... generate a tube

            if (GenerateTube(_profRad, _vertices, out sol))
            {
              // We now need to break the pipe...

              // If it was created, add it to our list to draw

              _created.Add(sol);
              sol = null;

              // Clear all but the last vertex to draw from
              // next time

              ClearAllButLast(_vertices, 1);
            }
          }
        }
        catch
        {
          // If the tube generation failed...

          if (sol != null)
          {
            sol.Dispose();
          }

          // Loop, creating the most recent successful tube we can

          bool succeeded = false;
          int n = 1;

          do
          {
            try
            {
              // Generate the previous, working tube using all
              // but the last points (if it fails, one more is
              // excluded per iteration, until we get a working
              // tube)

              GenerateTube(
                _profRad, GetAllButLast(_vertices, n++), out sol
              );

              _created.Add(sol);
              sol = null;
              succeeded = true;
            }
            catch { }
          }
          while (!succeeded && n < _vertices.Count);

          if (succeeded)
          {
            ClearAllButLast(_vertices, n - 1);

            if (_vertices.Count > 1)
            {
              try
              {
                // And generate a tube for the remaining vertices

                GenerateTube(_profRad, _vertices, out sol);
              }
              catch
              {
                succeeded = false;
              }
            }
          }
          
          if (!succeeded && sol != null)
          {
            sol.Dispose();
            sol = null;
          }
        }

        // Draw our solid(s)

        draw.SubEntityTraits.Color = ColorIndex;

        foreach (DBObject obj in _created)
        {
          Entity ent = obj as Entity;
          if (ent != null)
          {
            try
            {
              ent.WorldDraw(draw);
            }
            catch
            {}
          }
        }

        if (sol != null)
        {
          try
          {
            sol.WorldDraw(draw);
          }
          catch
          { }
        }

        if (_vertices.Count > 0)
        {
          Point3d lastPt = _vertices[_vertices.Count - 1];
          
          // Create a cursor sphere

          using (Solid3d cursor = new Solid3d())
          {
            try
            {
              cursor.CreateSphere(_profRad);

              if (cursor != null)
              {
                cursor.TransformBy(
                  Matrix3d.Displacement(lastPt - Point3d.Origin)
                );

                // Draw the cursor

                draw.SubEntityTraits.Color = ColorIndex;

                cursor.WorldDraw(draw);
              }
            }
            catch { }
          }
        }

        if (sol != null)
        {
          sol.Dispose();
        }
      }

      draw.SubEntityTraits.Color = origCol;

      return true;
    }
示例#7
0
        protected override bool WorldDrawData(WorldDraw draw)
        {
            if (!base.WorldDrawData(draw))
            {
                return(false);
            }

            short origCol = draw.SubEntityTraits.Color;

            if (_resizing)
            {
                using (Solid3d sphere = new Solid3d())
                {
                    try
                    {
                        sphere.CreateSphere(_profRad);

                        if (sphere != null)
                        {
                            sphere.TransformBy(
                                Matrix3d.Displacement(
                                    _resizeLocation - Point3d.Origin
                                    )
                                );

                            // Draw the cursor

                            draw.SubEntityTraits.Color = ColorIndex;
                            sphere.WorldDraw(draw);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        _doc.Editor.WriteMessage(
                            "\nException: {0} - {1}", ex.Message, ex.InnerException
                            );
                    }
                    finally
                    {
                        draw.SubEntityTraits.Color = origCol;
                    }
                }
                return(true);
            }

            // If we're currently drawing...

            if (_drawing)
            {
                Solid3d sol = null;
                try
                {
                    // If we have vertices that haven't yet been drawn...

                    if (_vertices.Count > 1 //&&
                        //_vertices.Count - 1 > _lastDrawnVertex
                        )
                    {
                        // ... generate a tube

                        if (GenerateTube(_profRad, _vertices, out sol))
                        {
                            // We now need to break the pipe...

                            // If it was created, add it to our list to draw

                            _created.Add(sol);
                            sol = null;

                            // Clear all but the last vertex to draw from
                            // next time

                            ClearAllButLast(_vertices, 1);
                        }
                    }
                }
                catch
                {
                    // If the tube generation failed...

                    if (sol != null)
                    {
                        sol.Dispose();
                    }

                    // Loop, creating the most recent successful tube we can

                    bool succeeded = false;
                    int  n         = 1;

                    do
                    {
                        try
                        {
                            // Generate the previous, working tube using all
                            // but the last points (if it fails, one more is
                            // excluded per iteration, until we get a working
                            // tube)

                            GenerateTube(
                                _profRad, GetAllButLast(_vertices, n++), out sol
                                );

                            _created.Add(sol);
                            sol       = null;
                            succeeded = true;
                        }
                        catch { }
                    }while (!succeeded && n < _vertices.Count);

                    if (succeeded)
                    {
                        ClearAllButLast(_vertices, n - 1);

                        if (_vertices.Count > 1)
                        {
                            try
                            {
                                // And generate a tube for the remaining vertices

                                GenerateTube(_profRad, _vertices, out sol);
                            }
                            catch
                            {
                                succeeded = false;
                            }
                        }
                    }

                    if (!succeeded && sol != null)
                    {
                        sol.Dispose();
                        sol = null;
                    }
                }

                // Draw our solid(s)

                draw.SubEntityTraits.Color = ColorIndex;

                foreach (DBObject obj in _created)
                {
                    Entity ent = obj as Entity;
                    if (ent != null)
                    {
                        try
                        {
                            ent.WorldDraw(draw);
                        }
                        catch
                        {}
                    }
                }

                if (sol != null)
                {
                    try
                    {
                        sol.WorldDraw(draw);
                    }
                    catch
                    { }
                }

                if (_vertices.Count > 0)
                {
                    Point3d lastPt = _vertices[_vertices.Count - 1];

                    // Create a cursor sphere

                    using (Solid3d cursor = new Solid3d())
                    {
                        try
                        {
                            cursor.CreateSphere(_profRad);

                            if (cursor != null)
                            {
                                cursor.TransformBy(
                                    Matrix3d.Displacement(lastPt - Point3d.Origin)
                                    );

                                // Draw the cursor

                                draw.SubEntityTraits.Color = ColorIndex;

                                cursor.WorldDraw(draw);
                            }
                        }
                        catch { }
                    }
                }

                if (sol != null)
                {
                    sol.Dispose();
                }
            }

            draw.SubEntityTraits.Color = origCol;

            return(true);
        }
示例#8
0
        public static void CreateSphere()
        {
            try
            {
                Document doc =
                    Application.DocumentManager.MdiActiveDocument;
                Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database;
                Editor ed = doc.Editor;

                Transaction tr =
                    doc.TransactionManager.StartTransaction();
                using (tr)
                {
                    BlockTable bt =
                        (BlockTable)tr.GetObject(
                            db.BlockTableId,
                            OpenMode.ForRead
                            );
                    BlockTableRecord btr =
                        (BlockTableRecord)tr.GetObject(
                            bt[BlockTableRecord.ModelSpace],
                            OpenMode.ForWrite
                            );
                    Solid3d sol = new Solid3d();
                    sol.CreateSphere(10.0);

                    const string matname =
                        "Sitework.Paving - Surfacing.Riverstone.Mortared";
                    DBDictionary matdict =
                        (DBDictionary)tr.GetObject(
                            db.MaterialDictionaryId,
                            OpenMode.ForRead
                            );
                    if (matdict.Contains(matname))
                    {
                        sol.Material = matname;
                    }
                    else
                    {
                        ed.WriteMessage(
                            "\nMaterial (" + matname + ") not found" +
                            " - sphere will be rendered without it.",
                            matname
                            );
                    }
                    btr.AppendEntity(sol);

                    tr.AddNewlyCreatedDBObject(sol, true);
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                PGA.MessengerManager.MessengerManager.AddLog(ex.Message);
            }

            using (var doc = Active.Document.LockDocument())
            {
                AcadUtilities.ZoomExtents();
            }
        }
示例#9
0
        public static Point3d DistFromPointToSolid(
            ref Document doc, ref Solid3d solid, Point3d point, double start, double step, ref double off, bool line)
        {
            var rez = new Point3d();

            using (var acTrans = doc.Database.TransactionManager.StartTransaction())
            {
                var acBlkTbl = (BlockTable)acTrans.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite);

                var acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                var SOLID = (Solid3d)solid.Clone();
                SOLID.SetDatabaseDefaults();
                acBlkTblRec.AppendEntity(SOLID);
                acTrans.AddNewlyCreatedDBObject(SOLID, true);


                var startR = start <= 0 ? step : start;

                var sphere = new Solid3d();
                sphere.SetDatabaseDefaults();
                sphere.CreateSphere(startR);
                sphere.TransformBy(Matrix3d.Displacement(Point3d.Origin.GetVectorTo(point)));
                sphere.SetDatabaseDefaults();
                acBlkTblRec.AppendEntity(sphere);
                acTrans.AddNewlyCreatedDBObject(sphere, true);


                while (sphere.CheckInterference(solid) == false)
                {
                    try
                    {
                        sphere.OffsetBody(step);
                        off += step;
                    }
                    catch
                    {
                    }
                }

                if (line)
                {
                    var counter = 0;

                    sphere.BooleanOperation(BooleanOperationType.BoolIntersect, SOLID);
                    var dbo = new DBObjectCollection();
                    sphere.Explode(dbo);

                    var coll       = new Point3dCollection();
                    var curvesColl = new DBObjectCollection();

                    var ent = dbo[0] as Entity;
                    if (ent.GetType().ToString().IndexOf("Surface") > 0)
                    {
                        var surf =
                            ent as Surface;
                        var ns = surf.ConvertToNurbSurface();
                        foreach (var nurb in ns)
                        {
                            double ustart   = nurb.UKnots.StartParameter,
                                     uend   = nurb.UKnots.EndParameter,
                                     uinc   = (uend - ustart) / nurb.UKnots.Count,
                                     vstart = nurb.VKnots.StartParameter,
                                     vend   = nurb.VKnots.EndParameter,
                                     vinc   = (vend - vstart) / nurb.VKnots.Count;

                            for (var u = ustart; u <= uend; u += uinc)
                            {
                                for (var v = vstart; v <= vend; v += vinc)
                                {
                                    coll.Add(nurb.Evaluate(u, v));
                                    counter++;
                                }
                            }
                        }
                        if (counter < 1)
                        {
                            var sub = new DBObjectCollection();
                            surf.Explode(sub);
                            foreach (Entity entt in sub)
                            {
                                acBlkTblRec.AppendEntity(entt);
                                acTrans.AddNewlyCreatedDBObject(entt, true);
                                curvesColl.Add(entt);
                            }
                        }
                    }
                    else
                    {
                        var surf = (Region)ent;
                        var p1   = surf.GeometricExtents.MaxPoint;
                        var p2   = surf.GeometricExtents.MinPoint;
                        var p    = new Point3d((p1.X + p2.X) / 2.0, (p1.Y + p2.Y) / 2.0, (p1.Z + p2.Z) / 2.0);
                        coll.Add(p);
                        coll.Add(p);
                        var sub = new DBObjectCollection();
                        surf.Explode(sub);
                        foreach (Entity entt in sub)
                        {
                            acBlkTblRec.AppendEntity(entt);
                            acTrans.AddNewlyCreatedDBObject(entt, true);
                            curvesColl.Add(entt);
                        }
                    }

                    foreach (DBObject ob in curvesColl)
                    {
                        var curve = (Curve)acTrans.GetObject(ob.Id, OpenMode.ForRead);
                        var p1    = curve.StartPoint;
                        var p2    = curve.EndPoint;
                        var p     = new Point3d((p1.X + p2.X) / 2.0, (p1.Y + p2.Y) / 2.0, (p1.Z + p2.Z) / 2.0);

                        coll.Add(p);
                        coll.Add(p1);
                        coll.Add(p2);
                    }

                    if (coll.Count > 0)
                    {
                        rez = coll[0];
                        foreach (Point3d p in coll)
                        {
                            if (p.DistanceTo(point) < rez.DistanceTo(point))
                            {
                                rez = p;
                            }
                        }
                    }
                    else
                    {
                        rez = point;
                    }
                }
                // acTrans.Commit();
            }
            return(rez);
        }
        protected override bool WorldDrawData(WorldDraw draw)
        {
            if (!base.WorldDrawData(draw))
            {
                return(false);
            }

            var ctm  = TransientManager.CurrentTransientManager;
            var ints = new IntegerCollection();

            while (_lineSegs.Count > 0)
            {
                // Get the line segment and remove it from the list

                var ls = _lineSegs[0];
                _lineSegs.RemoveAt(0);

                // Create an equivalent, red, database line
                // (or yellow, if calibrating)

                var ln = new Line(ls.StartPoint, ls.EndPoint);
                ln.ColorIndex = 1;
                _lines.Add(ln);

                // Draw it as transient graphics

                ctm.AddTransient(
                    ln, TransientDrawingMode.DirectShortTerm,
                    128, ints
                    );
            }

            if (_drawing)
            {
                if (_cursor == null)
                {
                    if (_vertices.Count > 0)
                    {
                        // Clear our skeleton

                        ClearTransients();

                        _curPt = _vertices[_vertices.Count - 1];

                        // Make our sphere 10cm in diameter (5cm radius)

                        var sol = new Solid3d();
                        sol.CreateSphere(0.05);
                        _cursor = sol;
                        _cursor.TransformBy(
                            Matrix3d.Displacement(_curPt - Point3d.Origin)
                            );

                        _cursor.ColorIndex = 2;

                        ctm.AddTransient(
                            _cursor, TransientDrawingMode.DirectShortTerm,
                            128, ints
                            );
                    }
                }
                else
                {
                    if (_vertices.Count > 0)
                    {
                        var newPt = _vertices[_vertices.Count - 1];
                        _cursor.TransformBy(
                            Matrix3d.Displacement(newPt - _curPt)
                            );
                        _curPt = newPt;

                        ctm.UpdateTransient(_cursor, ints);
                    }
                }
            }
            else // !_drawing
            {
                if (_cursor != null)
                {
                    ctm.EraseTransient(_cursor, ints);
                    _cursor.Dispose();
                    _cursor = null;
                }
            }

            return(true);
        }