public void Cmd_GenViews() { if (!LicensingAgent.Check()) { return; } var acCurDoc = Application.DocumentManager.MdiActiveDocument; var acCurDb = acCurDoc.Database; var acCurEd = acCurDoc.Editor; var modelView = acCurEd.GetCurrentView().Clone() as ViewTableRecord; var uSelOpts = new PromptSelectionOptions { MessageForAdding = "\nSelect objects to use in view creation: " }; var userSelection = acCurEd.GetSelection(uSelOpts); if (userSelection.Status != PromptStatus.OK) { return; } using (var acTrans = acCurDb.TransactionManager.StartTransaction()) { var boundBox = acTrans.GetBoundingBox(userSelection.Value.GetObjectIds(), acCurDb); //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 create views in?", AllowArbitraryInput = true }; var iterator = 'A'; var keyDict = new Dictionary <string, string>(); foreach (var layout in layoutList) { if (layout.LayoutName != "Model") { 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; } acCurEd.SwitchToPaperSpace(); var layOutSize = GetLayoutSize(chosenLayout); var importSize = new LayoutSize(0, 0); var iPorts = ImportViewports(acCurEd, ref importSize.Height, ref importSize.Width); var vStyles = acTrans.GetObject(acCurDb.VisualStyleDictionaryId, OpenMode.ForRead) as DBDictionary; if (iPorts.Count > 0) { foreach (var iPort in iPorts) { var ht = CalcUnit.GetProportion(iPort.VHeight, importSize.Height, layOutSize.Height); var wd = CalcUnit.GetProportion(iPort.VWidth, importSize.Width, layOutSize.Width); if (ht > layOutSize.Height || wd > layOutSize.Width) { continue; } var vPort = new Viewport { Height = ht, Width = wd }; var importPt = iPort.InsertPoint; var xProp = CalcUnit.GetProportion(importPt.X, importSize.Width, layOutSize.Width); var yProp = CalcUnit.GetProportion(importPt.Y, importSize.Height, layOutSize.Height); vPort.CenterPoint = new Point3d(xProp, yProp, 0); acCurDb.AppendEntity(vPort, acTrans); vPort.ViewDirection = iPort.ViewDirection; ZoomViewport(acCurDb, vPort, boundBox); //TODO find closest scale to zoom //TODO allow user to set these if (vStyles != null) { vPort.SetShadePlot(ShadePlotType.Hidden, vStyles.GetAt("Hidden")); } // Enable the viewport vPort.Visible = true; vPort.On = true; vPort.UpdateDisplay(); } } boundBox.Dispose(); LayoutManager.Current.CurrentLayout = "Model"; using (var view = acCurEd.GetCurrentView()) { view.CopyFrom(modelView); acCurEd.SetCurrentView(view); } try { Thread.Sleep(100); LayoutManager.Current.CurrentLayout = chosenLayout.LayoutName; } catch (Exception e) { Console.WriteLine(e); MailAgent.Report(e.Message); } if (modelView != null) { modelView.Dispose(); } acTrans.Commit(); } }
public void Cmd_PartsGen() { var acCurDoc = Application.DocumentManager.MdiActiveDocument; if (acCurDoc == null) { return; } var acCurDb = acCurDoc.Database; var acCurEd = acCurDoc.Editor; var modelView = acCurEd.GetCurrentView().Clone() as ViewTableRecord; var objIds = acCurEd.GetFilteredSelection(Enums.DxfNameEnum._3Dsolid, false); if (objIds.Length <= 0) { return; } var splitNum = 8; var colCount = splitNum / 2; var rowCount = 2; var partList = new List <Solid3d>(); using (var acTrans = acCurDb.TransactionManager.StartTransaction()) { foreach (var ob in objIds) { var acSol = acTrans.GetObject(ob, OpenMode.ForRead) as Solid3d; if (acSol == null) { continue; } partList.Add(acSol); } acTrans.Commit(); } var sortedParts = partList.OrderBy(e => e.GetPartName()).ToList(); var objChunks = sortedParts.ChunkBy(splitNum); var layName = "Model"; var vPorts = new List <Viewport>(); using (var pWorker = new ProgressAgent("Creating Part Views: ", sortedParts.Count)) using (var acTrans = acCurDb.TransactionManager.StartTransaction()) { for (var i = 0; i < objChunks.Count; i++) { var curLayout = "PrtGen"; var id = LayoutManager.Current.CreateAndMakeLayoutCurrentByAddition(curLayout); // Open the created layout var lay = (Layout)acTrans.GetObject(id, OpenMode.ForWrite); // Open the Block table record Paper space for write var acBlkTableRec = acTrans.GetObject(lay.BlockTableRecordId, OpenMode.ForWrite) as BlockTableRecord; // Make some settings on the layout and get its extents lay.SetPlotSettings( //"ISO_full_bleed_2A0_(1189.00_x_1682.00_MM)", // Try this big boy! // "ANSI_B_(11.00_x_17.00_Inches)", "ARCH_full_bleed_D_(36.00_x_24.00_Inches)", "monochrome.ctb", "AutoCAD PDF (High Quality Print).pc3" ); if (acBlkTableRec != null) { foreach (var vpId in acBlkTableRec) { if (vpId == acCurDb.PaperSpaceVportId) { continue; } var vp = acTrans.GetObject(vpId, OpenMode.ForRead) as Viewport; if (vp != null) { vPorts.Add(vp); } } var layOutSize = GetLayoutSize(lay); var paperLength = layOutSize.Width; var paperHeight = layOutSize.Height; var dSpacing = CalcUnit.GetProportion(1.5, 36, paperLength); var dBorder = CalcUnit.GetProportion(1.5, 36, paperLength); var availWidth = paperLength - dBorder * 2.5 - dSpacing * (colCount + 1); var availHeight = paperHeight - dBorder * 2.25 - dSpacing * (rowCount + 1); var dWidth = availWidth / colCount; var dWHalf = dWidth / 2; var dHeight = availHeight / rowCount; var dhHalf = dHeight / 2; var col1 = dBorder + dWHalf + dSpacing; var col2 = dBorder + dWHalf + dSpacing * 2 + dWidth; var col3 = dBorder + dWHalf + dSpacing * 3 + dWidth * 2; var col4 = dBorder + dWHalf + dSpacing * 4 + dWidth * 3; //var col5 = dBorder + dWHalf + dSpacing * 5 + dWidth * 4; var row1 = paperHeight - dhHalf - dBorder - dSpacing; var row2 = paperHeight - dhHalf - dBorder - dSpacing * 2 - dHeight; //row3 = paperHeight - dhHalf - dBorder - dSpacing * 3 - dHeight * 2; //row4 = paperHeight - dhHalf - dBorder - dSpacing * 4 - dHeight * 3; var positions = new[] { new Point2d(col1, row1), new Point2d(col2, row1), new Point2d(col3, row1), new Point2d(col4, row1), new Point2d(col1, row2), new Point2d(col2, row2), new Point2d(col3, row2), new Point2d(col4, row2) }; for (var j = 0; j < objChunks[i].Count; j++) { //Progress progress bar or exit if ESC has been pressed if (!pWorker.Progress()) { acTrans.Abort(); return; } var acSol = acTrans.GetObject(objChunks[i][j].ObjectId, OpenMode.ForWrite) as Solid3d; if (acSol == null) { continue; } var acExt = acSol.GeometricExtents; var acVport = new Viewport { CenterPoint = positions[j].Convert3D(), Width = dWidth, Height = dHeight, ViewDirection = ViewDirection.TopView }; acCurDb.AppendEntity(acVport, acTrans); acVport.FitContentToViewport(acExt); acVport.FitViewportToContent(acExt); acVport.CenterPoint = positions[j].Convert3D(); acVport.UpdateDisplay(); acVport.Layer = "Defpoints"; // Enable the viewport acVport.Visible = true; acVport.On = true; //acVport.CreateBaseViewFromVp(acSol.ObjectId, acCurEd, acCurDb, lay, acVport.CenterPoint); System.Threading.Thread.Sleep(300); vPorts.Add(acVport); //Insert Part Tags //Insert block to name the viewport var insertMin = acVport.GeometricExtents.MinPoint; var insertMax = acVport.GeometricExtents.MaxPoint; var mid = insertMin.GetMidPoint(insertMax); var midInsert = new Point3d(mid.X, positions[j].Convert3D().Y - 1, 0); // Open the Block table for read var acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) as BlockTable; if (!acBlkTbl.Has(SettingsInternal.TagName)) { MakeTag_Name(acBlkTbl, acTrans); } var blockName = SettingsInternal.TagName; var acBlkRef = acBlkTableRec.InsertBlock(blockName, midInsert, acCurDb); if (acBlkRef != null) { UpdatePartViewSubs(acBlkRef, acSol, acCurDoc); } } } layName = lay.LayoutName; } //Save the new objects to the database LayoutManager.Current.CurrentLayout = "MODEL"; using (var view = acCurEd.GetCurrentView()) { view.CopyFrom(modelView); acCurEd.SetCurrentView(view); } try { Thread.Sleep(300); LayoutManager.Current.CurrentLayout = layName; } catch (Exception e) { Console.WriteLine(e); } if (modelView != null) { modelView.Dispose(); } // Zoom so that we can see our new layout, again with a little padding acCurEd.Command("_.ZOOM", "_E"); acCurEd.Command("_.ZOOM", ".7X"); acCurEd.Regen(); // Commit the transaction acTrans.Commit(); } //using (var acTrans = acCurDb.TransactionManager.StartTransaction()) //{ // foreach (var acVp in vPorts) // { // if (acVp.ObjectId == acCurDb.PaperSpaceVportId) continue; // var delVp = acTrans.GetObject(acVp.ObjectId, OpenMode.ForWrite) as Viewport; // delVp.Erase(); // delVp.Dispose(); // } // acTrans.Commit(); //} }