public static void SelectCommand() { using (var tr = new QuickTransaction()) { var set = tr.GetImpliedOrSelect(); if (set == null) { return; } tr.WriteLine($"Count: " + set.Count); tr.SetImpliedSelection(set); tr.StringCommand("C2P "); tr.SetImpliedSelection(set = tr.SelectLast().Value ?? SelectionSet.FromObjectIds(new ObjectId[0])); tr.Commit(); } }
/// <summary> /// Method to convert viewport to View base /// </summary> /// <param name="prRes"></param> /// <param name="acVp"></param> /// <param name="acCurEd"></param> /// <param name="acCurDb"></param> /// <param name="curLayout"></param> /// <param name="insertPoint"></param> private void CreateBaseViewFromVp(PromptSelectionResult prRes, Viewport acVp, Editor acCurEd, Database acCurDb, Layout curLayout, Point3d insertPoint) { LayoutManager.Current.CurrentLayout = "Model"; var ss = SelectionSet.FromObjectIds(prRes.Value.GetObjectIds()); var scaleString = ViewportExtensions.GetScaleString(acVp.StandardScale); if (scaleString == "Custom" || scaleString == "1:1") { // ReSharper disable once SpecifyACultureInStringConversionExplicitly scaleString = acVp.CustomScale.ToString(); } var extents = acCurDb.TileMode ? new Extents3d(acCurDb.Extmin, acCurDb.Extmax) : (int)Application.GetSystemVariable("CVPORT") == 1 ? new Extents3d(acCurDb.Pextmin, acCurDb.Pextmax) : new Extents3d(acCurDb.Extmin, acCurDb.Extmax); using (var view = acCurEd.GetCurrentView()) { var viewTransform = Matrix3d.PlaneToWorld(acVp.ViewDirection) .PreMultiplyBy(Matrix3d.Displacement(view.Target - Point3d.Origin)) .PreMultiplyBy(Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target)) .Inverse(); extents.TransformBy(viewTransform); view.ViewDirection = acVp.ViewDirection; view.Width = (extents.MaxPoint.X - extents.MinPoint.X) * 1.2; view.Height = (extents.MaxPoint.Y - extents.MinPoint.Y) * 1.2; view.CenterPoint = new Point2d( (extents.MinPoint.X + extents.MaxPoint.X) / 2.0, (extents.MinPoint.Y + extents.MaxPoint.Y) / 2.0); acCurEd.SetCurrentView(view); } LayoutManager.Current.CurrentLayout = curLayout.LayoutName; acCurEd.Command("_VIEWBASE", "M", "T", "B", "E", "R", "ALL", "A", ss, string.Empty, "O", "C", insertPoint, "H", "V", "V", "I", "Y", "TA", "Y", "N", "X", "S", scaleString, string.Empty, string.Empty); }
buildSSetOX2d() { SelectionSet objSSet1 = default(SelectionSet); SelectionSet objSSet2 = default(SelectionSet); SelectionSet objSSetX = default(SelectionSet); TypedValue[] TVs = new TypedValue[1]; TVs[0] = new TypedValue((int)DxfCode.LayerName, "OX-AREAS-*"); objSSet1 = Select.buildSSet(TVs); ObjectIdCollection ids = default(ObjectIdCollection); if (objSSet1.Count > 0) { ids = new ObjectIdCollection(objSSet1.GetObjectIds()); } TVs[0] = new TypedValue((int)DxfCode.LayerName, "OX-LIM-OUTER"); objSSet2 = Select.buildSSet(TVs); if (objSSet2.Count > 0) { foreach (ObjectId id in objSSet2.GetObjectIds()) { ids.Add(id); } } ObjectId[] idss = new ObjectId[ids.Count]; for (int i = 0; i <= ids.Count - 1; i++) { idss[i] = ids[i]; } objSSetX = SelectionSet.FromObjectIds(idss); return(objSSetX); }
internal static void Traverse(bool reselect = false) { var acCurDoc = Application.DocumentManager.MdiActiveDocument; var acCurDb = acCurDoc.Database; var acCurEd = acCurDoc.Editor; //Start a selection set SelectionSet acSet; //Check for pick-first selection -> if none, get selection if (!acCurEd.CheckForPickFirst(out acSet)) { acSet = SelectionSet.FromObjectIds(acCurEd.GetFilteredSelection(Enums.DxfNameEnum._3Dsolid, false)); } using (var acTrans = acCurDb.TransactionManager.StartTransaction()) { using (var pWorker = new ProgressAgent("Parsing Solids: ", acSet.Count)) { var parseCount = 1; foreach (var objId in acSet.GetObjectIds()) { //Progress progress bar or exit if ESC has been pressed if (!pWorker.Progress()) { acTrans.Abort(); return; } var acSol = acTrans.GetObject(objId, OpenMode.ForRead) as Solid3d; if (acSol == null) { continue; } var entInfo = new EntInfo(acSol, acCurDb, acTrans); acSol.AddXData(entInfo, acCurDb, acTrans); if (!reselect) { acCurEd.WriteMessage("\n" + entInfo.PrintInfo(false, parseCount)); } parseCount++; #region Debug Testing //acSol.UpdateXData("TestName", Enums.XDataCode.Name, acCurDb, acTrans); //1 //acSol.UpdateXData("TestInfo", Enums.XDataCode.Info, acCurDb, acTrans); //2 //acSol.UpdateXData(10, Enums.XDataCode.Length, acCurDb, acTrans); //3 //acSol.UpdateXData(20, Enums.XDataCode.Width, acCurDb, acTrans); //4 //acSol.UpdateXData(30, Enums.XDataCode.Thickness, acCurDb, acTrans); //5 //acSol.UpdateXData(40, Enums.XDataCode.Volume, acCurDb, acTrans); //6 //acSol.UpdateXData(50, Enums.XDataCode.MaxArea, acCurDb, acTrans); //7 //acSol.UpdateXData(60, Enums.XDataCode.MaxPerimeter, acCurDb, acTrans); //8 //acSol.UpdateXData(70, Enums.XDataCode.Asymmetry, acCurDb, acTrans); //9 //acSol.UpdateXData("AxymV", Enums.XDataCode.AsymmetryVector, acCurDb, acTrans); //10 //acSol.UpdateXData(1, Enums.XDataCode.PartOf, acCurDb, acTrans); //11 //acSol.UpdateXData(2, Enums.XDataCode.PartTotal, acCurDb, acTrans); //12 //acSol.UpdateXData(3, Enums.XDataCode.NumChanges, acCurDb, acTrans); //13 //acSol.UpdateXData(false, Enums.XDataCode.IsSweep, acCurDb, acTrans); //14 //acSol.UpdateXData(true, Enums.XDataCode.IsMirror, acCurDb, acTrans); //15 //acSol.UpdateXData(false, Enums.XDataCode.HasHoles, acCurDb, acTrans); //16 //acSol.UpdateXData(Enums.Texture.Across, Enums.XDataCode.Texture, acCurDb, acTrans); //17 //acSol.UpdateXData(Enums.Production.MillingTwoSide, Enums.XDataCode.Production, acCurDb, acTrans); //18 //acSol.UpdateXData(acSol.Handle, Enums.XDataCode.ParentObject, acCurDb, acTrans); //19 //acSol.UpdateXData(new List<Handle>() {acSol.Handle, acSol.Handle , acSol.Handle}, //Enums.XDataCode.ChildObjects, acCurDb, acTrans); //20 //acCurEd.WriteMessage("\n Testing XData Read"); //acCurEd.WriteMessage("\n" + acSol.GetAppName()); //acCurEd.WriteMessage("\n" + acSol.GetPartName()); //acCurEd.WriteMessage("\n" + acSol.GetPartInfo()); //acCurEd.WriteMessage("\n" + acSol.GetPartLength()); //acCurEd.WriteMessage("\n" + acSol.GetPartWidth()); //acCurEd.WriteMessage("\n" + acSol.GetPartThickness()); //acCurEd.WriteMessage("\n" + acSol.GetPartVolume()); //acCurEd.WriteMessage("\n" + acSol.GetPartArea()); //acCurEd.WriteMessage("\n" + acSol.GetPartPerimeter()); //acCurEd.WriteMessage("\n" + acSol.GetPartAsymmetry()); //acCurEd.WriteMessage("\n" + acSol.GetAsymVector()); //acCurEd.WriteMessage("\n" + acSol.GetQtyOf()); //acCurEd.WriteMessage("\n" + acSol.GetQtyTotal()); //acCurEd.WriteMessage("\n" + acSol.GetNumChanges()); //acCurEd.WriteMessage("\n" + acSol.GetIsSweep()); //acCurEd.WriteMessage("\n" + acSol.GetIsMirror()); //acCurEd.WriteMessage("\n" + acSol.GetHasHoles()); //acCurEd.WriteMessage("\n" + acSol.GetTextureDirection()); //acCurEd.WriteMessage("\n" + acSol.GetProductionType()); //acCurEd.WriteMessage("\n" + acSol.GetParent()); //acCurEd.WriteMessage("\n" + String.Join(",",acSol.GetChildren())); #endregion } if (reselect) { acCurEd.SetImpliedSelection(acSet.GetObjectIds()); } } acTrans.Commit(); } }
public void Cmd_NameParts() { if (!LicensingAgent.Check()) { return; } var acCurDoc = Application.DocumentManager.MdiActiveDocument; var acCurDb = acCurDoc.Database; var acCurEd = acCurDoc.Editor; var keyList = new List <KeywordAgent>(); var userResetPartCounter = SettingsUser.ResetPartCount; //Reset Part Number to 1 if specified in bool if (SettingsUser.ResetPartCount) { SortingAgent.CurrentPartNumber = 1; } var keyAgentName = new KeywordAgent(acCurEd, "NameFormat", "Enter naming format: ", TypeCode.String, SettingsUser.NamingConvention); var keyAgentNum = new KeywordAgent(acCurEd, "StartFrom", "Enter part number to start from: ", TypeCode.Int32, SortingAgent.CurrentPartNumber.ToString()); keyList.Add(keyAgentName); keyList.Add(keyAgentNum); //Check for pick-first selection -> if none, get selection var acSet = SelectionSet.FromObjectIds(acCurEd.GetFilteredSelection(DxfNameEnum._3Dsolid, false, keyList)); keyAgentName.Set(ref SettingsUser.NamingConvention); keyAgentNum.Set(ref SortingAgent.CurrentPartNumber); using (var acTrans = acCurDb.TransactionManager.StartTransaction()) { using (var pWorker = new ProgressAgent("Parsing Solids: ", acSet.Count)) { var eList = new List <EntInfo>(); foreach (var objId in acSet.GetObjectIds()) { //Progress progress bar or exit if ESC has been pressed if (!pWorker.Progress()) { acTrans.Abort(); return; } var acSol = acTrans.GetObject(objId, OpenMode.ForRead) as Solid3d; if (acSol == null) { continue; } eList.Add(new EntInfo(acSol, acCurDb, acTrans)); } //eList.SortSolids(); eList.SortAndName(pWorker, acCurDb, acCurEd, acTrans); } acTrans.Commit(); } SettingsUser.ResetPartCount = userResetPartCounter; }
public void Cmd_RCBOM() { var acCurDoc = Application.DocumentManager.MdiActiveDocument; var acCurDb = acCurDoc.Database; var acCurEd = acCurDoc.Editor; //Check for pick-first selection -> if none, get selection var acSet = SelectionSet.FromObjectIds(acCurEd.GetFilteredSelection(Enums.DxfNameEnum._3Dsolid, false)); var multAmount = 1; if (SettingsUser.PromptForMultiplication) { multAmount = acCurEd.GetPositiveInteger("\nEnter number to multiply parts by: ", 1); } using (var acTrans = acCurDb.TransactionManager.StartTransaction()) { //Get all Layouts in the Drawing var layoutList = new List <Layout>(); var dbDict = (DBDictionary)acTrans.GetObject(acCurDb.LayoutDictionaryId, OpenMode.ForRead); foreach (var curEntry in dbDict) { var layout = (Layout)acTrans.GetObject(curEntry.Value, OpenMode.ForRead); if (layout != null) { layoutList.Add(layout); } } var pKeyOpts = new PromptKeywordOptions(string.Empty) { Message = "\nWhich layout would you like to insert the parts list in?", AllowArbitraryInput = true }; var iterator = 'A'; var keyDict = new Dictionary <string, string>(); foreach (var layout in layoutList) { keyDict.Add(layout.LayoutName, iterator.ToString()); pKeyOpts.Keywords.Add(iterator.ToString(), iterator.ToString(), iterator + ": " + layout.LayoutName.ToLower()); iterator++; } pKeyOpts.AllowNone = false; var pKeyRes = acCurEd.GetKeywords(pKeyOpts); if (pKeyRes.Status != PromptStatus.OK) { return; } var returnIterator = pKeyRes.StringResult; ObjectId id; var layoutName = string.Empty; foreach (var entry in keyDict) { if (entry.Value == returnIterator) { layoutName = entry.Key; break; } } if (dbDict.Contains(layoutName)) { id = dbDict.GetAt(layoutName); } else { acCurEd.WriteMessage("\nLayout not found. Cannot continue."); acTrans.Abort(); return; } var chosenLayout = acTrans.GetObject(id, OpenMode.ForRead) as Layout; if (chosenLayout == null) { return; } // Reference the Layout Manager var acLayoutMgr = LayoutManager.Current; // Set the layout current if it is not already if (chosenLayout.TabSelected == false) { acLayoutMgr.CurrentLayout = chosenLayout.LayoutName; } if (chosenLayout.LayoutName != "Model") { try { acCurEd.SwitchToPaperSpace(); } catch (System.Exception e) { Console.WriteLine(e); } } // Ask for the table insertion point var pr = acCurEd.GetPoint("\nEnter table insertion point: "); if (pr.Status == PromptStatus.OK) { using (var pWorker = new ProgressAgent("Parsing Solids: ", acSet.Count)) { var eList = new List <EntInfo>(); foreach (var objId in acSet.GetObjectIds()) { //Progress progress bar or exit if ESC has been pressed if (!pWorker.Progress()) { acTrans.Abort(); return; } var acSol = acTrans.GetObject(objId, OpenMode.ForRead) as Solid3d; if (acSol == null) { continue; } eList.Add(new EntInfo(acSol, acCurDb, acTrans)); } var acTable = new Table(); acTable.Position = pr.Value; acTable.TableStyle = acCurDb.Tablestyle; eList.SortToTable(pWorker, acCurDb, acCurEd, acTrans, acTable, multAmount); acCurDb.AppendEntity(acTable, acTrans); //Generate the layout acTable.GenerateLayout(); acTrans.MoveToAttachment(acTable, SettingsUser.TableAttach, pr.Value, SettingsUser.TableXOffset, SettingsUser.TableYOffset); } } acTrans.Commit(); } }
public static void testForUnionPlines() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; // get argument to choose boolean operation mode PromptKeywordOptions pko = new PromptKeywordOptions("\nChoose boolean operation mode " + "[Union/Subtract]: ", "Union Subtract"); // The default depends on our current settings pko.Keywords.Default = "Union"; PromptResult pkr = ed.GetKeywords(pko); if (pkr.Status != PromptStatus.OK) { return; } string choice = pkr.StringResult; bool doUnion = choice == "Union" ? true : false; List <Region> regLst = new List <Region>(); List <Polyline> delPline = new List <Polyline>(); using (DocumentLock doclock = doc.LockDocument()) { //start a transaction using (Transaction tr = db.TransactionManager.StartTransaction()) { TypedValue[] tvs = new TypedValue[3] { new TypedValue(0, "lwpolyline"), new TypedValue(-4, "&"), new TypedValue(70, 1) }; SelectionFilter filter = new SelectionFilter(tvs); PromptSelectionOptions pso = new PromptSelectionOptions(); pso.MessageForRemoval = "\nSelect closed polylines only: "; pso.MessageForAdding = "\nSelect closed polylines: "; PromptSelectionResult result = ed.GetSelection(filter); if (result.Status != PromptStatus.OK) { return; } try { SelectionSet sset = result.Value; ObjectId[] ids = sset.GetObjectIds(); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false); Region objreg1 = new Region(); for (int n = 0; n < ids.Count(); n++) { DBObject obj = tr.GetObject(ids[n], OpenMode.ForRead) as DBObject; Polyline pline1 = obj as Polyline; if (pline1 == null) { return; } // Add the polyline to the List to rerase them all at the end of execution delPline.Add(pline1); // Add the polyline to the array DBObjectCollection objArray1 = new DBObjectCollection(); objArray1.Add(pline1); // create the 1 st region DBObjectCollection objRegions1 = new DBObjectCollection(); objRegions1 = Region.CreateFromCurves(objArray1); objreg1 = objRegions1[0] as Region; btr.AppendEntity(objreg1); tr.AddNewlyCreatedDBObject(objreg1, true); objreg1.ColorIndex = 1;//optional // add the region to the List<Region> for the future work regLst.Add(objreg1); } //ed.WriteMessage("\nCount regions:\t{0}\n", regLst.Count);//just for the debug // sort regions by areas Region[] items = regLst.ToArray(); Array.Sort(items, (Region x, Region y) => y.Area.CompareTo(x.Area)); // get the biggest region first Region mainReg = items[0]; // ed.WriteMessage("\nMain region area:\t{0:f3}\n", items[0].Area);//just for the debug if (!mainReg.IsWriteEnabled) { mainReg.UpgradeOpen(); } if (items.Length == 2) { if (!doUnion) { mainReg.BooleanOperation(BooleanOperationType.BoolSubtract, (Region)items[1]); } else { mainReg.BooleanOperation(BooleanOperationType.BoolUnite, (Region)items[1]); } } else { // starting iteration from the second region int i = 1; do { Region reg1 = items[i]; Region reg2 = items[i + 1]; if ((reg1 == null) || (reg2 == null)) { break; } else { // subtract region 1 from region 2 if (reg1.Area > reg2.Area) { // subtract the smaller region from the larger one // reg1.BooleanOperation(BooleanOperationType.BoolUnite, reg2); if (!doUnion) { mainReg.BooleanOperation(BooleanOperationType.BoolSubtract, reg1); } else { mainReg.BooleanOperation(BooleanOperationType.BoolUnite, reg1); } } else { // subtract the smaller region from the larger one reg2.BooleanOperation(BooleanOperationType.BoolUnite, reg1); if (!doUnion) { mainReg.BooleanOperation(BooleanOperationType.BoolSubtract, reg2); } else { mainReg.BooleanOperation(BooleanOperationType.BoolUnite, reg2); } } } // increase counter i++; } while (i < items.Length - 1); } mainReg.ColorIndex = 1;// put dummy color for region // erase polylines foreach (Polyline poly in delPline) { if (poly != null) { if (!poly.IsWriteEnabled) { poly.UpgradeOpen(); } poly.Erase(); if (!poly.IsDisposed) { poly.Dispose(); } } } // --- explode region and create polyline from exploded entities --- // DBObjectCollection regexpl = new DBObjectCollection(); mainReg.Explode(regexpl); List <ObjectId> exids = new List <ObjectId>(); // gather selected object into the List<ObjectId> if (regexpl.Count > 0) { foreach (DBObject obj in regexpl) { Entity ent = obj as Entity; if (ent != null) { ObjectId eid = btr.AppendEntity(ent); tr.AddNewlyCreatedDBObject(ent, true); exids.Add(eid); } } } // define AcadDocument as object ObjectId[] entids = new ObjectId[] { }; Array.Resize(ref entids, exids.Count); // convert List<ObjectId> to array of ObjectID exids.CopyTo(entids, 0); ed.Regen(); // create a new selection set and exploded items SelectionSet newset = SelectionSet.FromObjectIds(entids); ed.SetImpliedSelection(newset.GetObjectIds()); PromptSelectionResult pfres = ed.SelectImplied(); // execute Sendcommand synchronously Quick.Editor.Command(new object[] { "select\n" }); // execute Sendcommand synchronously string cmd = "_pedit _M _P" + " " + "" + " " + "_J" + " " + "" + " " + "" + "\n"; Quick.Editor.Command(new object[] { cmd }); // rerase region if this is do not erased (relative to current DELOBJ variable value) if (mainReg != null) { if (!mainReg.IsWriteEnabled) { mainReg.UpgradeOpen(); } } mainReg.Erase(); tr.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage("\nAutoCAD exception:\n" + ex.Message + "\n" + ex.StackTrace); } finally { ed.WriteMessage("\n{0}", new Autodesk.AutoCAD.Runtime.ErrorStatus().ToString());//optional, might be removed } } } }
public void Cmd_LayParts() { if (!LicensingAgent.Check()) { return; } var acCurDoc = Application.DocumentManager.MdiActiveDocument; var acCurDb = acCurDoc.Database; var acCurEd = acCurDoc.Editor; var keys = new List <KeywordAgent>(); var keyFlatAssembly = new KeywordAgent(acCurEd, "FLatshot", "Flatten part to 2D lines? ", TypeCode.Boolean, SettingsUser.LayFlatShot.ToSpecified()); keys.Add(keyFlatAssembly); //Check for pick-first selection -> if none, get selection var acSet = SelectionSet.FromObjectIds(acCurEd.GetFilteredSelection(Enums.DxfNameEnum._3Dsolid, false, keys)); if (acSet.Count <= 0) { return; } keyFlatAssembly.Set(ref SettingsUser.LayFlatShot); if (SettingsUser.LayFlatShot) { var layFlatShot = acCurEd.GetBool("\n Create flatshot for each side?"); if (layFlatShot != null) { SettingsUser.LayAllSidesFlatShot = layFlatShot.Value; } var retainHidden = acCurEd.GetBool("\n Retain hidden lines?"); if (retainHidden != null) { SettingsUser.RetainHiddenLines = retainHidden.Value; } } else { SettingsUser.LayAllSidesFlatShot = false; } //Set the UCS to World - save the user UCS var userCoordSystem = acCurEd.CurrentUserCoordinateSystem; acCurEd.CurrentUserCoordinateSystem = Matrix3d.Identity; //Set the Grid Mode to ON if it is not ON - Get the users current grid mode var userGridMode = AcVars.GridMode; if (AcVars.GridMode != Enums.GridMode.On) { AcVars.GridMode = Enums.GridMode.On; } acCurEd.Regen(); var laypt = acCurEd.Get2DPoint("\nSelect point to lay parts at: "); // Return the Grid Mode to the Users Setting AcVars.GridMode = userGridMode; var multAmount = 1; if (SettingsUser.PromptForMultiplication) { multAmount = acCurEd.GetPositiveInteger("\nEnter number to multiply parts by: ", 1); } using (var acTrans = acCurDb.TransactionManager.StartTransaction()) { using (var pWorker = new ProgressAgent("Parsing Solids: ", acSet.Count)) { var eList = new List <EntInfo>(); foreach (var objId in acSet.GetObjectIds()) { //Progress progress bar or exit if ESC has been pressed if (!pWorker.Progress()) { acTrans.Abort(); return; } var acSol = acTrans.GetObject(objId, OpenMode.ForRead) as Solid3d; if (acSol == null) { continue; } eList.Add(new EntInfo(acSol, acCurDb, acTrans)); } eList.SortAndLay(laypt, pWorker, acCurDb, acCurEd, acTrans, multAmount); } acTrans.Commit(); } //Set the UCS back to the user UCS acCurEd.CurrentUserCoordinateSystem = userCoordSystem; acCurEd.Regen(); }