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(); } } }
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(); } } }
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); }