public static void AlignBlocks() { try { var options = new PromptEntityOptions("\nSelect polyline: "); options.SetRejectMessage("Objet non valide."); options.AddAllowedClass(typeof(Polyline), true); var result = Quick.Editor.GetEntity(options); if (result.Status != PromptStatus.OK) { return; } var lineId = result.ObjectId; var sel = Quick.GetImpliedOrSelect(); if (result.Status != PromptStatus.OK) { return; } var db = lineId.Database; foreach (var blockId in sel.GetObjectIds()) { using (var trans = db.TransactionManager.StartTransaction()) { try { var line = trans.GetObject(lineId, OpenMode.ForRead) as Polyline; var blockRef = trans.GetObject(blockId, OpenMode.ForWrite) as BlockReference; if (blockRef == null) { continue; } var blockpos = blockRef.Position; // better use the center point, instead min/max var pointOverLine = line.GetClosestPointTo(blockRef.Position, false); //var vectorto = pointOverLine.GetVectorTo(blockRef.Position); //var ang = vectorto.AngleOnPlane() blockRef.Position = pointOverLine; // move // assuming a well behaved 2D block aligned with XY //Vector3d lineDirection = line.GetFirstDerivative(pointOverLine); //double angleToRotate = Vector3d.XAxis.GetAngleTo(lineDirection, Vector3d.ZAxis); //angel between block to the nearest point var b2near = blockpos.Convert2d(new Plane()).GetVectorTo(pointOverLine.Convert2d(new Plane())).Angle *Rad2Deg; //var pos1 = Math.Atan2(blockpos.Y - pointOverLine.Y, pointOverLine.X - blockpos.X) * Rad2Deg; //var angeltoblock = lineDirection.GetAngleTo(blockRef.Position.GetAsVector())*Rad2Deg; blockRef.Rotation = (b2near - 90) * Deg2Rad; //-90 to convert to block plane (0 is downwards). trans.Commit(); } catch (Exception ex) { Quick.WriteLine(ex.Message + "\n" + ex.StackTrace); } } } Quick.SetSelected(sel); } catch (Exception ex) { Quick.WriteLine(ex.Message + "\n" + ex.StackTrace); } }
public static void WindowSwapCommand() { var imp = Quick.GetImpliedOrSelect(); if (imp == null) { Quick.WriteLine("[ws] No objects were selected."); return; } var all = Quick.SelectAll(); if (all == null) { Quick.WriteLine("[ws] Failed selecting All."); return; } var rest = all.Cast <SelectedObject>().ExceptBy(imp.Cast <SelectedObject>(), o => o.ObjectId.Handle.Value).Select(o => o.ObjectId).ToSelectionSet(SelectionMethod.Crossing); Quick.SetSelected(rest); }
public static void WindowOnlyInsideCommand() { var imp = Quick.GetImpliedOrSelect(); if (imp == null) { Quick.WriteLine("[ww] No objects were selected."); return; } Quick.ClearSelected(); var all = Quick.GetImpliedOrSelect(); if (all == null) { Quick.WriteLine("[ww] Failed selecting Other."); return; } var rest = all.Cast <SelectedObject>().IntersectBy(imp.Cast <SelectedObject>(), o => o.ObjectId.Handle.Value).Select(o => o.ObjectId).ToSelectionSet(SelectionMethod.Crossing); Quick.SetSelected(rest); }
public static void MRotateCommand() { using (var tr = new QuickTransaction()) { // objects initializing var nomutt = Convert.ToInt32(Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("nomutt")); try { // Get the current document and database // Start a transaction // Open the Block table for read var _sel = Quick.GetImpliedOrSelect(); if (_sel == null || _sel.Count == 0) { tr.Editor.WriteMessage("Nothing was selected."); return; } var howmuch = tr.Editor.GetAngle("How many degrees (Anti Clockwise) to add (Negative will go Clockwise)"); if (howmuch.Status == PromptStatus.Cancel) { return; } var curUCSMatrix = tr.Doc.Editor.CurrentUserCoordinateSystem; var curUCS = curUCSMatrix.CoordinateSystem3d; foreach (SelectedObject o in _sel) { var obj = (BlockReference)tr.GetObject(o.ObjectId) ?? throw new NullReferenceException("obj"); var matrix = Matrix3d.Rotation(howmuch.Value, curUCS.Zaxis, obj.Position); obj.TransformBy(matrix); } // Add the new object to the block table record and the transaction // Save the new objects to the database tr.Commit(); Quick.SetSelected(_sel); } catch (Exception ex) { Quick.WriteLine(ex.Message + "\n" + ex.StackTrace); } finally { Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("nomutt", nomutt); } } }
public static SelectionSet SetSelected(this SelectionSet ss) { Quick.SetSelected(ss); return(ss); }
public static void SetSelected(this QuickTransaction tr, SelectionSet ss, bool runsssetfirst = false) { Quick.SetSelected(ss, runsssetfirst); }
public static void MagicReplaceCommand() { // objects initializing var nomutt = Convert.ToInt32(Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("nomutt")); var doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument; var ed = doc.Editor; var db = doc.Database; try { using (doc.LockDocument()) { using (var tr = new QuickTransaction()) { var toreplace = tr.GetImpliedOrSelect(new PromptSelectionOptions()); if (toreplace == null) { return; } ed.WriteMessage("\nSelect destinion block: "); var totype = Quick.SelectSingle(); ed.WriteMessage("\n"); if (totype == null) { return; } var masterblock = (BlockTableRecord)tr.GetObject(((BlockReference)tr.GetObject(totype ?? ObjectId.Null, OpenMode.ForWrite)).BlockTableRecord, OpenMode.ForRead); var bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; var @new = bt[masterblock.Name]; Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("nomutt", 0); var notparsed = 0; var flattern = false; var objs = toreplace.Cast <SelectedObject>().Select(o => tr.GetObject(o.ObjectId, OpenMode.ForRead) as Entity).ToArray(); if (objs.Any(o => (o as BlockReference)?.Position.Z > 0)) { flattern = Quick.AskQuestion("Should flattern blocks with Z value", true) ?? false; } var os = new List <ObjectId>(); foreach (var ent in objs) { var oldblk = ent as BlockReference; if (oldblk == null) { //not block.. notparsed++; continue; } var p = oldblk.Position; var ip = flattern ? new Point3d(p.X, p.Y, 0) : p; var scl = oldblk.ScaleFactors; var rot = oldblk.Rotation; var newblk = new BlockReference(ip, @new); newblk.SetPropertiesFrom(ent); newblk.Rotation = rot; newblk.ScaleFactors = scl; tr.BlockTableRecordCurrentSpace.AppendEntity(newblk); tr.AddNewlyCreatedDBObject(newblk, true); ApplyAttributes(db, tr, newblk); oldblk.UpgradeOpen(); oldblk.Erase(); oldblk.Dispose(); os.Add(newblk.ObjectId); } Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("nomutt", 1); tr.Commit(); if (notparsed > 0) { ed.WriteMessage($"{notparsed} are not blocks and were not replaced.\n"); } ed.WriteMessage($"{toreplace.Count} were replaced to block {masterblock.Name} successfully.\n"); Quick.SetSelected(os.ToArray()); } } } catch (Exception ex) { ed.WriteMessage(ex.Message + "\n" + ex.StackTrace); } finally { Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("nomutt", nomutt); } }