Author Topic: Palette Problem  (Read 13463 times)

0 Members and 1 Guest are viewing this topic.

MickD

  • Gator
  • Posts: 3315
  • (x-in)->[process]->(y-out)
Re: Palette Problem
« Reply #30 on: August 17, 2006, 10:17:11 PM »
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:
Code: [Select]
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!
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: “To attain knowledge, add things
every day; to obtain wisdom, remove things every day.”

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Palette Problem
« Reply #31 on: August 17, 2006, 10:27:14 PM »
That's what I was expecting you to say Mick about the eNotOpenForWrite - have you tried opening for write whilst it is in your dialog?
It would need this as AutoCAD uses the open/close transaction mechanism...

Actually, on second look, it looks like your myEnt variable is going out of scope too early...
Me

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Palette Problem
« Reply #32 on: August 17, 2006, 10:31:33 PM »
Actually, on 3rd look :), your transaction has finished and been commited, so the object is closed.
Me

MickD

  • Gator
  • Posts: 3315
  • (x-in)->[process]->(y-out)
Re: Palette Problem
« Reply #33 on: August 17, 2006, 10:47:35 PM »
Doh!, it's always somethng obvious (to new eye's). I'll see if I can remedy it now. I may have to re-implement it into my app for updating some xdata which was the whole reasom for using it in the first place.
brb
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: “To attain knowledge, add things
every day; to obtain wisdom, remove things every day.”

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Palette Problem
« Reply #34 on: August 17, 2006, 10:54:43 PM »
Mick, I don't want to have to reproduce your project to test, so can you post the zipped project so I can play...
Me

MickD

  • Gator
  • Posts: 3315
  • (x-in)->[process]->(y-out)
Re: Palette Problem
« Reply #35 on: August 17, 2006, 11:15:47 PM »
No prob's, I had made a few changes before I caught you post but the original code is still above.
The changes -
Code: [Select]
private ObjectId m_entId;
private Autodesk.AutoCAD.Colors.Color m_colour;
private Database db = HostApplicationServices.WorkingDatabase;

public MyEnt(ObjectId entId) // takes an entity ObjectId
{
m_entId = entId; // get some info for the prop's grid:
using(Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = tr.GetObject(m_entId, OpenMode.ForWrite)as Entity;
m_colour = ent.Color;
}
}

// update the ent's prop's in the destructor(?)
~MyEnt()
{
using(Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = tr.GetObject(m_entId, OpenMode.ForWrite)as Entity;
ent.Color = m_colour; // set the new colour:
}
}
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: “To attain knowledge, add things
every day; to obtain wisdom, remove things every day.”

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Palette Problem
« Reply #36 on: August 18, 2006, 12:05:51 AM »
Did you get it to work Mick?
Me

MickD

  • Gator
  • Posts: 3315
  • (x-in)->[process]->(y-out)
Re: Palette Problem
« Reply #37 on: August 18, 2006, 12:16:05 AM »
Nope, I put it down after the last post, bigger fish to fry at the moment :). It wouldn't be a bad thing to implement for xdata, it can make the data look like real properties of the objects selected.
I'm just using a simple dilaog at the moment that works fine and is not confused with the proper properties dialog used by acad, i'm happy with that but others may well find a good use for this approach I'd imagine, Dave's grid may be the best answer.
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: “To attain knowledge, add things
every day; to obtain wisdom, remove things every day.”

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Palette Problem
« Reply #38 on: August 18, 2006, 12:27:01 AM »
I got it to work by axing your desctructor (as in your implementation, you don't know when it will be called - maybe wrap in a using statement or dispose of it the form close down).

Good to see you went objectid's as well - that's the way I would have done it.

Cheers,
Glenn.
Me

MickD

  • Gator
  • Posts: 3315
  • (x-in)->[process]->(y-out)
Re: Palette Problem
« Reply #39 on: August 18, 2006, 12:37:34 AM »
I use id's liberally now since I've had to pass them back and forth to arx/managed and it works very well.
I thought of setting the prop's in the set() method of the MyEnt property, is that what you?
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: “To attain knowledge, add things
every day; to obtain wisdom, remove things every day.”

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Palette Problem
« Reply #40 on: August 18, 2006, 01:34:49 AM »
Yep - I used objectIds for class stuff in ARX as well.

Yes, I did it in the 'set'.
Me

DBARANAS

  • Guest
Re: Palette Problem
« Reply #41 on: August 18, 2006, 01:37:28 AM »
I have been playing with the property grid example.

I really like that you don't have to manage row and collumns...this is becoming tedious as I plug it in everywhere. I added an array and it shows all the elements and it was no more code to do that!

A good use for that grid is displaying my classes for debugging, I got 1 of my big classes running in the grid I posted, but it was 1000 boring lines of code.

I am trying to get the property grid on a form that goes on a pallette. I get the palette but not the grid. Surprizingly it pops up a separate  acad property palette.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PropertyGridTest
{
   public partial class frmProperties : Form
   {
      private AppSettings _appSettings = null;

      public frmProperties()
      {

         InitializeComponent();
         _appSettings = new AppSettings();

      }

      public frmProperties(AppSettings appSettings) : this()
      {
         propertyGrid1.SelectedObject = appSettings;
      }

      private void InitializeComponent()
      {
         this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
         this.SuspendLayout();
         //
         //propertyGrid1
         //
         this.propertyGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
         this.propertyGrid1.Location = new System.Drawing.Point(0, 0);
         this.propertyGrid1.Name = "propertyGrid1";
         this.propertyGrid1.Size = new System.Drawing.Size(252, 916);
         this.propertyGrid1.TabIndex = 1;
         //
         //frmProperties
         //
         this.ClientSize = new System.Drawing.Size(252, 916);
         this.Controls.Add(this.propertyGrid1);
         this.Name = "frmProperties";
         this.ResumeLayout(false);

      }
      private System.Windows.Forms.PropertyGrid propertyGrid1;
   }

}


MickD

  • Gator
  • Posts: 3315
  • (x-in)->[process]->(y-out)
Re: Palette Problem
« Reply #42 on: August 18, 2006, 01:45:29 AM »
The best way to go is create a user control instead of a form to host the property grid and set the user control in your palette's load method.
Sing out if you need a hand.
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: “To attain knowledge, add things
every day; to obtain wisdom, remove things every day.”

DBARANAS

  • Guest
Re: Palette Problem
« Reply #43 on: August 18, 2006, 02:20:07 AM »
I changed it back to a user control and changed the new sub and it works.

I made a class public to fit into the app. I got lucky on this one!!

   public PropertyGridTest.frmProperties ctlProperties = new PropertyGridTest.frmProperties();
   public PropertyGridTest.AppSettings PropertyItems = new PropertyGridTest.AppSettings();

   public void createPaletteProperties()
   {

      pProperties = new Autodesk.AutoCAD.Windows.PaletteSet("Properties", new Guid("99741830-3C35-42ed-8008-B27226C9A188"));
      pProperties.Style = PaletteSetStyles.ShowCloseButton == false;
      pProperties.Style = PaletteSetStyles.ShowTabForSingle == false;
      PropertyItems = new PropertyGridTest.AppSettings();
      ctlProperties = new PropertyGridTest.frmProperties();
      pProperties.Add("Properties", ctlProperties);
      pProperties.Size = new System.Drawing.Size(260, 950);
      pProperties.Dock = Autodesk.AutoCAD.Windows.DockSides.Right;
      pProperties.KeepFocus = true;
      pProperties.Visible = false;

   }

using System.Windows.Forms;

namespace PropertyGridTest
{
   public partial class frmProperties : System.Windows.Forms.UserControl
   {
      //Inherits Form
      private AppSettings _appSettings = null;

      public frmProperties()
      {
         InitializeComponent();
         propertyGrid1.SelectedObject = PropertyItems;
      }

      private void InitializeComponent()
      {
         this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
         this.SuspendLayout();
         //
         //propertyGrid1
         //
         this.propertyGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
         this.propertyGrid1.Location = new System.Drawing.Point(0, 0);
         this.propertyGrid1.Name = "propertyGrid1";
         this.propertyGrid1.Size = new System.Drawing.Size(252, 916);
         this.propertyGrid1.TabIndex = 1;
         //
         //frmProperties
         //
         this.ClientSize = new System.Drawing.Size(252, 916);
         this.Controls.Add(this.propertyGrid1);
         this.Name = "frmProperties";
         this.ResumeLayout(false);

      }
      private System.Windows.Forms.PropertyGrid propertyGrid1;
   }

}

MickD

  • Gator
  • Posts: 3315
  • (x-in)->[process]->(y-out)
Re: Palette Problem
« Reply #44 on: August 18, 2006, 02:24:18 AM »
*woot!*  :kewl:
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: “To attain knowledge, add things
every day; to obtain wisdom, remove things every day.”