Ok, this is an unorthodox way of setting entity prop's but I needed it to get and set some xdata but this simple app shows what I was trying to do.
The prop's dialog code and a class to work with:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
namespace CsMgdAcad2
{
// My class for testing, we have to get and set the prop's
// ourselves as these ents may not have .net attributes
// available.
public class MyEnt
{
private Entity m_ent;
private Autodesk.AutoCAD.Colors.Color m_colour;
public MyEnt(ref Entity ent) // takes an entity reference
{
m_ent = ent; // get some info for the prop's grid:
m_colour = ent.Color; // we'll use the colorindex prop.
}
// this is the logical place to change the values
// from the props grid while dialog still open(maybe not ?)
~MyEnt()
{
m_ent.Color = m_colour; // set the new colour:
}
// set some prop's for the prop's grid:
[CategoryAttribute("Object Data"),
DescriptionAttribute("Name of Section")]
public Autodesk.AutoCAD.Colors.Color Colour
{
get
{
return m_colour;
}
set
{
m_colour = value;
}
}
}
/// <summary>
/// Summary description for Props.
/// </summary>
public class Props : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Props()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(16, 8);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(256, 400);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText;
//
// Props
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 414);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.propertyGrid1});
this.Name = "Props";
this.Text = "Props";
this.Load += new System.EventHandler(this.Props_Load);
this.ResumeLayout(false);
}
#endregion
private void Props_Load(object sender, System.EventArgs e)
{
Database db =
HostApplicationServices.WorkingDatabase;
Editor ed =
Autodesk.AutoCAD.ApplicationServices.
Application.DocumentManager.MdiActiveDocument.Editor;
Transaction tr =
db.TransactionManager.StartTransaction();
MyEnt myent;
try
{
PromptEntityResult res =
ed.GetEntity("\nPick an entity to get the info:");
Entity ent =
tr.GetObject(res.ObjectId, OpenMode.ForWrite)as Entity;
if(ent != null)
{
myent = new MyEnt(ref ent);
propertyGrid1.SelectedObject = myent;
}
tr.Commit();
}
catch
{
}
finally
{
tr.Dispose();
}
}
}
}
if I try and set the colour of the ent directly in the property's set() I get a eNotOpenForWrite error and a crash!