예제 #1
0
파일: RcSlice.cs 프로젝트: 15831944/RabCab
        public void Cmd_RcSlice()
        {
            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);

            if (userSel == null)
            {
                return;
            }

            var prSelOpts = new PromptDistanceOptions("\nEnter slice distance: ")
            {
                AllowNone     = false,
                AllowZero     = false,
                AllowNegative = false,
                DefaultValue  = SettingsUser.RcSliceDepth
            };

            //Get the offset distance
            var prSelRes = acCurEd.GetDistance(prSelOpts);


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

            SettingsUser.RcSliceDepth = prSelRes.Value;

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

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

                    var faceEnt = acSol.GetSubentity(userSel.Item2);

                    using (var tempSurf = faceEnt.CreateSurfaceFromFace(acCurDb, acTrans, false))
                    {
                        var sliceSurf = Surface.CreateOffsetSurface(tempSurf, -SettingsUser.RcSliceDepth) as Surface;

                        //Sleep for 100 milliseconds to bypass an error caused by appending too qickly
                        Thread.Sleep(100);

                        Solid3d sliceSol = null;

                        try
                        {
                            sliceSol = acSol?.Slice(sliceSurf, true);
                            sliceSol?.SetPropertiesFrom(acSol);
                            acCurDb.AppendEntity(sliceSol, acTrans);
                            sliceSurf?.Dispose();
                        }
                        catch (Exception)
                        {
                            try //If the slice by surface method failed, use the slice by offset method
                            {
                                //Dispose of the unused surface and solids
                                sliceSol?.Dispose();
                                sliceSurf?.Dispose();

                                //Clone the input solid
                                var           subtSol = acSol?.Clone() as Solid3d;
                                SubentityId[] subIds  = { userSel.Item2 };

                                //Offset the cloned solids face and append it to the database
                                subtSol?.OffsetFaces(subIds, -SettingsUser.RcSliceDepth);

                                acCurDb.AppendEntity(subtSol, acTrans);

                                //Subtract the offset solid from the input solid, but don't delete it
                                Debug.Assert(acSol != null, "acSol != null");
                                Debug.Assert(subtSol != null, "subtSol != null");
                                new[] { acSol.ObjectId }.SolidSubtrahend(new[] { subtSol.ObjectId }, acCurDb, acTrans,
                                                                         false);
                            }
                            catch (Exception e)
                            {
                                //If nothing worked, dispose of everything and inform the user
                                sliceSol?.Dispose();
                                sliceSurf?.Dispose();
                                faceEnt.Dispose();
                                acCurEd.WriteMessage(e.Message);

                                acTrans.Abort();
                            }
                        }
                    }

                    faceEnt.Dispose();
                    acTrans.Commit();
                }
            }
            catch (Exception e)
            {
                acCurEd.WriteMessage(e.Message);
            }
        }
예제 #2
0
        private void method_0(ObjectId[] objectId_0, ProgressMeter progressMeter_0, CoordinateSystem coordinateSystem_0)
        {
            Database       workingDatabase = HostApplicationServices.WorkingDatabase;
            Editor         editor          = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            List <Solid3d> list            = new List <Solid3d>();
            double         epsilon         = Convert.ToDouble(CMD_FacesToSolid.string_2);
            bool           flag            = true;

            if (CMD_FacesToSolid.string_0 == "N")
            {
                flag = false;
            }
            DBManager.SetEpsilon();
            List <Triangle> list2 = Conversions.ToCeometricAcDbTriangleList(objectId_0);

            if (!Conversions.IsWCS(coordinateSystem_0))
            {
                Triangle.TransformCoordinates(list2, CoordinateSystem.Global(), coordinateSystem_0);
            }
            list2.Sort(new Triangle.CompareMinX());
            int    num  = 0;
            double num2 = -1.7976931348623157E+308;
            double num3 = -1.7976931348623157E+308;
            double num4 = 1.7976931348623157E+308;
            double num5 = 1.7976931348623157E+308;

            for (int i = list2.Count - 1; i >= 0; i--)
            {
                if (list2[i].NormalVector.IsOrthogonalTo(new ngeometry.VectorGeometry.Vector3d(0.0, 0.0, 1.0)))
                {
                    list2.RemoveAt(i);
                    num++;
                }
                else
                {
                    double minimumX = list2[i].MinimumX;
                    if (minimumX < num4)
                    {
                        num4 = minimumX;
                    }
                    double maximumX = list2[i].MaximumX;
                    if (maximumX > num2)
                    {
                        num2 = maximumX;
                    }
                    double minimumY = list2[i].MinimumY;
                    if (minimumY < num5)
                    {
                        num5 = minimumY;
                    }
                    double maximumY = list2[i].MaximumY;
                    if (maximumY > num3)
                    {
                        num3 = maximumY;
                    }
                }
            }
            ngeometry.VectorGeometry.Vector3d vector3d = new ngeometry.VectorGeometry.Vector3d(0.5 * (num4 + num2), 0.5 * (num5 + num3), 0.0);
            double num6 = 0.5 * Math.Max(Math.Abs(num2 - num4), Math.Abs(num3 - num5));
            double num7 = Math.Ceiling(100000.0 / num6);

            for (int j = 0; j < list2.Count; j++)
            {
                list2[j] = list2[j].Move(-1.0 * vector3d);
            }
            SubDMeshHandler subDMeshHandler = new SubDMeshHandler(list2);

            subDMeshHandler.BuildDataStructure(false);
            int k = 2147483647;

            while (k > 0)
            {
                this.messageFilter_0.CheckMessageFilter();
                k    = subDMeshHandler.HealXY(epsilon);
                num += k;
            }
            editor.WriteMessage("\nDegenerate projections healed: " + num);
            Autodesk.AutoCAD.Geometry.Matrix3d matrix3d  = Autodesk.AutoCAD.Geometry.Matrix3d.Scaling(num7, new Point3d(0.0, 0.0, 0.0));
            Autodesk.AutoCAD.Geometry.Matrix3d matrix3d2 = Autodesk.AutoCAD.Geometry.Matrix3d.Scaling(1.0 / num7, new Point3d(0.0, 0.0, 0.0));
            Autodesk.AutoCAD.Geometry.Matrix3d matrix3d3 = Autodesk.AutoCAD.Geometry.Matrix3d.Displacement(new Autodesk.AutoCAD.Geometry.Vector3d(vector3d.X, vector3d.Y, 0.0));
            progressMeter_0 = new ProgressMeter();
            progressMeter_0.SetLimit(objectId_0.Length);
            progressMeter_0.Start("Extruding objects");
            int num8 = 0;
            int num9 = 0;

            for (int l = 0; l < subDMeshHandler.FaceVertexIndex1.Count; l++)
            {
                try
                {
                    progressMeter_0.MeterProgress();
                    this.messageFilter_0.CheckMessageFilter((long)l, 1000);
                }
                catch (System.Exception ex)
                {
                    for (int m = 0; m < list.Count; m++)
                    {
                        list[m].Dispose();
                    }
                    progressMeter_0.Stop();
                    throw;
                }
                try
                {
                    Face   face = new Face(new Point3d(subDMeshHandler.Vertices[subDMeshHandler.FaceVertexIndex1[l]].X, subDMeshHandler.Vertices[subDMeshHandler.FaceVertexIndex1[l]].Y, subDMeshHandler.Vertices[subDMeshHandler.FaceVertexIndex1[l]].Z), new Point3d(subDMeshHandler.Vertices[subDMeshHandler.FaceVertexIndex2[l]].X, subDMeshHandler.Vertices[subDMeshHandler.FaceVertexIndex2[l]].Y, subDMeshHandler.Vertices[subDMeshHandler.FaceVertexIndex2[l]].Z), new Point3d(subDMeshHandler.Vertices[subDMeshHandler.FaceVertexIndex3[l]].X, subDMeshHandler.Vertices[subDMeshHandler.FaceVertexIndex3[l]].Y, subDMeshHandler.Vertices[subDMeshHandler.FaceVertexIndex3[l]].Z), false, false, false, false);
                    double z    = face.GetVertexAt(0).Z;
                    double z2   = face.GetVertexAt(1).Z;
                    double z3   = face.GetVertexAt(2).Z;
                    short  num10;
                    if (CMD_FacesToSolid.double_0 < 0.0)
                    {
                        if (z <= z2 && z <= z3)
                        {
                            num10 = 0;
                        }
                        else if (z2 <= z && z2 <= z3)
                        {
                            num10 = 1;
                        }
                        else
                        {
                            num10 = 2;
                        }
                    }
                    else if (z >= z2 && z >= z3)
                    {
                        num10 = 0;
                    }
                    else if (z2 >= z && z2 >= z3)
                    {
                        num10 = 1;
                    }
                    else
                    {
                        num10 = 2;
                    }
                    Point3d vertexAt = face.GetVertexAt(num10);
                    Point3d point3d  = new Point3d(vertexAt.X, vertexAt.Y, vertexAt.Z + CMD_FacesToSolid.double_0);
                    //point3d..ctor(vertexAt.X, vertexAt.Y, vertexAt.Z + CMD_FacesToSolid.double_0);
                    Autodesk.AutoCAD.DatabaseServices.Line line = new Autodesk.AutoCAD.DatabaseServices.Line(vertexAt, point3d);
                    Region region = this.method_2(subDMeshHandler, l);
                    if (region == null)
                    {
                        num8++;
                    }
                    else
                    {
                        region.TransformBy(matrix3d);
                        line.TransformBy(matrix3d);
                        try
                        {
                            Solid3d solid3d = new Solid3d();
                            solid3d.ExtrudeAlongPath(region, line, 0.0);
                            solid3d.SetPropertiesFrom(face);
                            list.Add(solid3d);
                        }
                        catch (System.Exception ex)
                        {
                            num9++;
                        }
                        line.Dispose();
                        region.Dispose();
                    }
                }
                catch (System.Exception ex)
                {
                }
            }
            editor.WriteMessage("\nNumber of failed projections: " + num8.ToString());
            editor.WriteMessage("\nNumber of failed extrusions : " + num8.ToString());
            progressMeter_0.Stop();
            if (flag && list.Count > 1)
            {
                progressMeter_0 = new ProgressMeter();
                int limit = (int)Math.Ceiling(Math.Log((double)list.Count) / Math.Log(2.0));
                progressMeter_0.SetLimit(limit);
                progressMeter_0.Start("Building solid");
                this.method_1(ref list, ref progressMeter_0);
            }
            using (Transaction transaction = workingDatabase.TransactionManager.StartTransaction())
            {
                BlockTable       blockTable       = (BlockTable)transaction.GetObject(workingDatabase.BlockTableId, (OpenMode)1);
                BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], (OpenMode)1);
                for (int n = 0; n < list.Count; n++)
                {
                    list[n].TransformBy(matrix3d2);
                    list[n].TransformBy(matrix3d3);
                    CoordinateTransformator coordinateTransformator = new CoordinateTransformator(coordinateSystem_0, CoordinateSystem.Global());
                    if (!coordinateTransformator.bool_0)
                    {
                        list[n].TransformBy(coordinateTransformator.ToAcadTransformation());
                    }
                    blockTableRecord.AppendEntity(list[n]);
                    transaction.AddNewlyCreatedDBObject(list[n], true);
                }
                transaction.Commit();
            }
            progressMeter_0.Stop();
        }