0 Members and 1 Guest are viewing this topic.
[CommandMethod("CreateCoordinateTable","cct", CommandFlags.Modal | CommandFlags.UsePickSet)] public void CreateMyTable() { // based on code written by Kean Walmsley Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; List<double[]> verts = new List<double[]>(); TypedValue[] values = new TypedValue[] { new TypedValue(0, "POLYLINE") }; SelectionFilter filter = new SelectionFilter(values); PromptSelectionOptions opts = new PromptSelectionOptions(); opts.MessageForRemoval = "\nMust be a type of 3dpoly!"; opts.MessageForAdding = "\nSelect the single 3dpoly: "; opts.SingleOnly = true; opts.SinglePickInSpace = true; opts.AllowDuplicates = false; try { PromptSelectionResult result = ed.GetSelection(opts, filter); if (result.Status != PromptStatus.OK) return; ed.WriteMessage("\nSelected: {0}\n", result.Value[0].GetType().Name); SelectedObject obj = result.Value[0]; using (Transaction tr = db.TransactionManager.StartTransaction()) { DBObject dbo = tr.GetObject(obj.ObjectId, OpenMode.ForRead,false) as DBObject; Polyline3d poly = dbo as Polyline3d; if (poly == null) { ed.WriteMessage("\nNo 3dPoly"); return; } foreach (ObjectId vId in poly) { PolylineVertex3d v3d = (PolylineVertex3d)tr.GetObject(vId, OpenMode.ForRead); Point3d pt = v3d.Position; verts.Add(new double[] { pt.X, pt.Y, pt.Z }); } BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); Table tbl = new Table(); tbl.TableStyle = db.Tablestyle; tbl.Position = ed.GetPoint("\nPick a table position: ").Value; TableStyle ts = (TableStyle)tr.GetObject(tbl.TableStyle, OpenMode.ForRead); double textht = ts.TextHeight(RowType.DataRow); int rows = verts.Count; int columns = verts[0].Length; //insert rows tbl.InsertRows(1, textht * 2, rows); // insert columns tbl.InsertColumns(1, textht * 15, columns-1);// first column is already exist, thus we'll have 3 columns //create range to merge the cells in the first row CellRange range = CellRange.Create(tbl, 0, 0, 0, columns-1); tbl.MergeCells(range); // set style for title row tbl.Cells[0, 0].Style = "Title"; tbl.Cells[0, 0].TextString = "Coordinate Table"; tbl.Rows[0].Height = textht * 2; tbl.InsertRows(1, textht * 2, 1); // set style for header row tbl.Rows[1].Style = "Header"; tbl.Rows[1].Height = textht * 1.5; string[] headers = { "X", "Y", "Z" }; //create contents in the first cell and set textstring tbl.Cells[1, 0].Contents.Add(); tbl.Cells[1, 0].Contents[0].TextString = headers[0]; for (int c = 1; c < columns; c++) { //for all of the rest cells just set textstring (or value) tbl.Cells[1, c].TextString = headers[c]; } for (int r = 2; r < rows + 2; r++)//exact number of data rows + title row + header row { // set style for data row tbl.Rows[r].Style = "Data"; tbl.Rows[r].Height = textht * 1.25; } // set column widths foreach (Column col in tbl.Columns) col.Width = textht * 15; //change last column values just to show data formatting // to set numeric values with precision of 3 decimals: // create DataTypeParameter object // set data type,set value, then data format for every cell: DataTypeParameter dtp = new DataTypeParameter(); dtp.DataType = DataType.Double; dtp.UnitType = UnitType.Unitless; // or UnitType.Unitless //populate column with values: int ct = 0; for (int r = 2; r < rows + 2; r++) //exact number of data rows + title row + header row { for (int c = 0; c < columns; c++) { tbl.Cells[r, c].Contents.Add(); tbl.Cells[r, c].Contents[0].DataFormat = "%lu2%pr3%th44";//or "%lu2%pr3%" tbl.Cells[r, c].Contents[0].DataType = dtp; tbl.Cells[r, c].Contents[0].SetValue(verts[ct][c], ParseOption.ParseOptionNone); } ct += 1; } tbl.GenerateLayout(); btr.AppendEntity(tbl); tr.AddNewlyCreatedDBObject(tbl, true); tr.Commit(); } } catch (System.Exception ex) { ed.WriteMessage(ex.Message + "\n" + ex.StackTrace); } }