예제 #1
0
        public static void SliceABox()
        {
            // 获取当前文档和数据库,启动事务
            var acDoc   = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            var acCurDb = acDoc.Database;

            using (var acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;

                // 以写模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;

                // 创建3D箱体
                var acSol3D = new Solid3d();
                acSol3D.CreateBox(5, 7, 10);
                acSol3D.ColorIndex = 7;

                // 3D实体的中心点放在(5,5,0)
                acSol3D.TransformBy(Matrix3d.Displacement(new Point3d(5, 5, 0) -
                                                          Point3d.Origin));

                // 将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acSol3D);
                acTrans.AddNewlyCreatedDBObject(acSol3D, true);

                // 定义镜像平面
                var acPlane = new Plane(new Point3d(1.5, 7.5, 0),
                                        new Point3d(1.5, 7.5, 10),
                                        new Point3d(8.5, 2.5, 10));

                var acSol3DSlice = acSol3D.Slice(acPlane, true);
                acSol3DSlice.ColorIndex = 1;

                // 将新对象添加到块表记录和事务
                acBlkTblRec.AppendEntity(acSol3DSlice);
                acTrans.AddNewlyCreatedDBObject(acSol3DSlice, true);

                // 提交事务
                acTrans.Commit();
            }
        }
예제 #2
0
        public void Cmd_EdgeBand()
        {
            if (!LicensingAgent.Check())
            {
                return;
            }
            var acCurDoc = Application.DocumentManager.MdiActiveDocument;
            var acCurDb  = acCurDoc.Database;
            var acCurEd  = acCurDoc.Editor;

            //Call user to select a face
            var userSel = acCurEd.SelectSubentity(SubentityType.Face, "\nSelect a FACE to use as cutting criteria: ");

            if (userSel == null)
            {
                return;
            }
            if (userSel.Item1 == ObjectId.Null)
            {
                return;
            }
            if (userSel.Item2 == SubentityId.Null)
            {
                return;
            }

            var insetOpts = new PromptDistanceOptions("\nEnter edge banding thickness: ")
            {
                AllowNone     = false,
                AllowZero     = false,
                AllowNegative = false,
                DefaultValue  = SettingsUser.EdgeBandThickness
            };

            //Get the offset distance
            var insetRes = acCurEd.GetDistance(insetOpts);

            if (insetRes.Status != PromptStatus.OK)
            {
                return;
            }

            SettingsUser.EdgeBandThickness = insetRes.Value;


            // ReSharper disable once CompareOfFloatsByEqualityOperator
            if (SettingsUser.EdgeBandThickness <= 0)
            {
                return;
            }

            Entity  faceEnt  = null;
            Surface tempSurf = null;
            Solid3d tempSol  = null;

            try
            {
                //Open a transaction
                using (var acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    var acSol = acTrans.GetObject(userSel.Item1, OpenMode.ForWrite) as Solid3d;

                    if (acSol == null)
                    {
                        acTrans.Abort();
                        return;
                    }

                    var innerSol = acSol.Clone() as Solid3d;

                    if (innerSol == null)
                    {
                        acTrans.Abort();
                        return;
                    }

                    acSol.Layer = acCurDb.GetCLayer(acTrans);
                    acCurDb.AppendEntity(innerSol);

                    faceEnt = acSol.GetSubentity(userSel.Item2);

                    var eInfo    = new EntInfo(acSol, acCurDb, acTrans);
                    var largestM = eInfo.GetLargestMeasurement();

                    using (tempSurf = faceEnt.CreateSurfaceFromFace(acCurDb, acTrans, false))
                    {
                        var thickness = largestM + SettingsUser.EdgeBandThickness;

                        using (tempSol = tempSurf.Thicken(-(thickness * 2), true))
                        {
                            tempSol.OffsetBody(-SettingsUser.EdgeBandThickness);

                            var cutSol = tempSol.Slice(tempSurf, true);
                            cutSol.Dispose();

                            acSol.BooleanOperation(BooleanOperationType.BoolSubtract, tempSol);
                        }
                    }

                    var acBool1 = new[] { innerSol.ObjectId };
                    var acBool2 = new[] { acSol.ObjectId };

                    acBool1.SolidSubtrahend(acBool2, acCurDb, acTrans, false);

                    acTrans.Commit();
                }
            }
            catch (Exception e)
            {
                acCurEd.WriteMessage(e.Message);
                MailAgent.Report(e.Message);
            }
            finally
            {
                if (faceEnt != null)
                {
                    faceEnt.Dispose();
                }
                if (tempSurf != null)
                {
                    tempSurf.Dispose();
                }
                if (tempSol != null)
                {
                    tempSol.Dispose();
                }
            }
        }
예제 #3
0
파일: RcICut.cs 프로젝트: 15831944/RabCab
        public void Cmd_ICut()
        {
            var acCurDoc = Application.DocumentManager.MdiActiveDocument;
            var acCurDb  = acCurDoc.Database;
            var acCurEd  = acCurDoc.Editor;

            //Call user to select a face
            var userSel = acCurEd.SelectSubentity(SubentityType.Face, "\nSelect a FACE to use as cutting criteria: ");

            if (userSel == null)
            {
                return;
            }
            if (userSel.Item1 == ObjectId.Null)
            {
                return;
            }
            if (userSel.Item2 == SubentityId.Null)
            {
                return;
            }

            var insetOpts = new PromptDistanceOptions("\nEnter inset distance: ")
            {
                AllowNone     = false,
                AllowZero     = false,
                AllowNegative = false,
                DefaultValue  = SettingsUser.RcICutInset
            };

            //Get the offset distance
            var insetRes = acCurEd.GetDistance(insetOpts);

            if (insetRes.Status != PromptStatus.OK)
            {
                return;
            }

            SettingsUser.RcICutInset = insetRes.Value;

            var prSelOpts2 = new PromptDistanceOptions("\nEnter cut depth: ")
            {
                AllowNone     = false,
                AllowZero     = false,
                AllowNegative = false,
                DefaultValue  = SettingsUser.RcICutDepth
            };

            //Get the offset distance
            var prDepthRes = acCurEd.GetDistance(prSelOpts2);

            if (prDepthRes.Status != PromptStatus.OK)
            {
                return;
            }

            SettingsUser.RcICutDepth = prDepthRes.Value;

            // ReSharper disable once CompareOfFloatsByEqualityOperator
            if (SettingsUser.RcICutDepth <= 0 || SettingsUser.RcICutInset <= 0)
            {
                return;
            }

            Entity  faceEnt  = null;
            Surface tempSurf = null;
            Solid3d tempSol  = null;

            try
            {
                //Open a transaction
                using (var acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    var acSol = acTrans.GetObject(userSel.Item1, OpenMode.ForWrite) as Solid3d;
                    if (acSol == null)
                    {
                        acTrans.Abort();
                        return;
                    }

                    faceEnt = acSol.GetSubentity(userSel.Item2);

                    using (tempSurf = faceEnt.CreateSurfaceFromFace(acCurDb, acTrans, false))
                    {
                        var thickness = SettingsUser.RcICutDepth + SettingsUser.RcICutInset;

                        using (tempSol = tempSurf.Thicken(-(thickness * 2), true))
                        {
                            tempSol.OffsetBody(-SettingsUser.RcICutInset);

                            var cutSol = tempSol.Slice(tempSurf, true);
                            cutSol.Dispose();

                            acSol.BooleanOperation(BooleanOperationType.BoolSubtract, tempSol);
                        }
                    }

                    acTrans.Commit();
                }
            }
            catch (Exception e)
            {
                acCurEd.WriteMessage(e.Message);
            }
            finally
            {
                if (faceEnt != null)
                {
                    faceEnt.Dispose();
                }
                if (tempSurf != null)
                {
                    tempSurf.Dispose();
                }
                if (tempSol != null)
                {
                    tempSol.Dispose();
                }
            }
        }