using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace ObservableSelectionSample
{
public class SelectionViewModel : INotifyPropertyChanged
{
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName) =>
PropertyChanged
?.Invoke(this,
new PropertyChangedEventArgs
(propertyName
)); #endregion
// Type of item of the ObsevableCollection.
public struct CurveGroup
{
// Properties bound to the ListView columns
public int Count { get; set; }
public double Length { get; set; }
public string Name { get; set; }
}
DataItemCollection itemCollection;
double total;
// Property bound to the ListView
public ObservableCollection<CurveGroup> Selection { get; }
// Property bound to the TextBlock
public double Total
{
get { return total; }
set { total = value; NotifyPropertyChanged(nameof(Total)); }
}
// Initialization and subscription to CollectionChanged and ItemsChanged events of the itemCollection
public SelectionViewModel()
{
Selection
= new ObservableCollection
<CurveGroup
>(); itemCollection = Application.UIBindings.Collections.Selection;
itemCollection.CollectionChanged += (s, e) => Update();
itemCollection.ItemsChanged += (s, e) => Update();
}
// Updating Selection and Total properties.
private void Update()
{
bool IsValidCurve(RXClass rXClass) =>
rXClass
.IsDerivedFrom(RXObject
.GetClass(typeof(Curve
))) && rXClass.DxfName != "RAY" && rXClass.DxfName != "XLINE";
Selection.Clear();
using (var tr
= new OpenCloseTransaction
()) {
var groups = itemCollection
.Cast<IDataItem>()
.Where(item => IsValidCurve(item.ObjectId.ObjectClass))
.Select(item => (Curve)tr.GetObject(item.ObjectId, OpenMode.ForRead))
.GroupBy(curve => curve.GetType().Name)
.Select(group => new CurveGroup
{
Count = group.Count(),
Length = group.Sum(c => c.GetDistanceAtParameter(c.EndParam)),
Name = group.Key
});
foreach (var group in groups)
Selection.Add(group);
Total = Selection.Sum(x => x.Length);
}
}
string txtRad;
/// <summary>
/// Gets the Command object bound to the Radius button (>).
/// </summary>
public RelayCommand RadiusCommandz
=> new RelayCommand
((_
) => Gettable
(),
(_
) => true);
/// <summary>
/// Method called by GetRadiusCommand.
/// </summary>
private void Gettable()
{
//----------------------------- insert table -------------------------------------//
var ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
PromptPointOptions ppo
= new PromptPointOptions
("\nPick insertion point of table: ");
PromptPointResult pres = ed.GetPoint(ppo);
if (pres.Status != PromptStatus.OK) return;
Point3d inspt = pres.Value;
// CreateTable(table, inspt);
}
//--------------------------------------------------------------------------------------------//
public static void CreateTable(List<List<string>> tabledata, Point3d insPt)
{
// completely borrowed from Kean Walmsley here:
// http://through-the-interface.typepad.com/through_the_interface/2011/11/handling-protocol-changes-to-autocads-table-in-net.html
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
string ver = ((string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("acadver")).Substring(0, 2);
int acver = Convert.ToInt32(ver);
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
// Set some constants that we'll use to create our tables
int numRows = tabledata.Count;
int numCols = tabledata.ElementAt(1).Count;
double txtHeight = db.Textsize;
double rowHeight = txtHeight * 1.7;
double colWidth = txtHeight * 30;
double horMarg = rowHeight / 6;
double verMarg = rowHeight / 4;
// We'll add our tables to the current space
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
// We'll use scoping to let us use the same variable
// name(s) for both tables we're creating
// for A2009
if (acver == 17)
{
// Create a table with the old protocol
tb.TableStyle = db.Tablestyle;
// Resize the table, adding rows and columns
tb.NumRows = numRows;
tb.NumColumns = numCols;
// Set the column width and row height
tb.SetColumnWidth(colWidth);
tb.SetRowHeight(rowHeight);
// Set the horizontal and vertical margins
tb.HorizontalCellMargin = horMarg;
tb.VerticalCellMargin = verMarg;
// Make sure the header and title rows are visible
tb.IsHeaderSuppressed = false;
tb.IsTitleSuppressed = false;
// Set the title string
tb.SetTextString(0, 0, tabledata.ElementAt(0)[0]);
// Set the text height for the full table: value of 7
// is bit-encoded Data (1), Title (2) and Header (4)
tb.SetTextHeight(txtHeight, 7);
// Populate the contents of the header and data sections
for (int i = 1; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
// Contents is A-E for the header an 0-n for the data
tb.SetTextString(i, j, tabledata.ElementAt(i - 1)[j]);
}
}
tb.GenerateLayout();
tb.Position = insPt;
btr.AppendEntity(tb);
tr.AddNewlyCreatedDBObject(tb, true);
}
// for A2010 >
else
if (acver == 18)
{
// Create a table with the new protocol
tb.TableStyle = db.Tablestyle;
// Set the height and width of the initial row and
// column belonging by default to the blank Table
tb.Rows[0].Height = rowHeight;
tb.Columns[0].Width = colWidth;
// Add the remaining rows and columns
tb.InsertRows(1, rowHeight, numRows - 1);
tb.InsertColumns(1, colWidth, numCols - 1);
// To query the number of rows and columns...
//
// int nRows = tb.Rows.Count;
// int nCols = tb.Columns.Count;
// To "suppress" the title and header, find the row
// with a style of "Title" or "Header" and set it to ""
tb.Cells[0, -1].Style = "Title";
tb.Cells[1, -1].Style = "Header";
// Add the contents of the Title cell
Cell tc = tb.Cells[0, 0];
tc.Contents.Add();
tc.Contents[0].TextHeight = txtHeight;
tc.Contents[0].TextString = tabledata.ElementAt(0)[0];
// Populate the contents of the header and data sections
for (int i = 1; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
// Contents is A-E for the header an 0-n for the data
string contents = tabledata.ElementAt(i)[j];
Cell c = tb.Cells[i, j];
// Set the text contents of the cell
c.Contents.Add();
c.Contents[0].TextHeight = txtHeight;
c.Contents[0].TextString = contents;
// Set the horizontal margins
c.Borders.Left.Margin = horMarg;
c.Borders.Right.Margin = horMarg;
// Set the vertical margins
c.Borders.Top.Margin = verMarg;
c.Borders.Bottom.Margin = verMarg;
}
}
// We'll separate this table from the first one
tb.Position = insPt;
tb.GenerateLayout();
btr.AppendEntity(tb);
tr.AddNewlyCreatedDBObject(tb, true);
}
tr.Commit();
}
}
//--------------------------------------------------------------------------------------------//
}
}