November 2014

Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            










« Using standard AutoCAD dialogs to select colors, linetypes and lineweights with .NET | Main | The 2009 product family has been announced »

February 12, 2008

Modifying the color, linetype and lineweight of an AutoCAD entity using standard dialogs from .NET

In the last post we saw code to display and use AutoCAD's built-in colour, linetype and lineweight dialogs. In this post we extend that by using each of them in sequence to display various properties of an entity, allowing the user to modify them.

While this is slightly more "real world" than the last post, it doesn't really make sense to implement a command such as the one below (the property palette is much better, for instance :-). The main purpose is to show how to set the initial values in the various dialogs and afterwards to make use of the values the user selects.

Here's the C# code:

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Windows;


namespace EntityProperties

{

  public class Commands

  {

    [CommandMethod("SPE")]

    public void SetPropertiesOnEntity()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;


      PromptEntityResult per =

        ed.GetEntity(

          "\nSelect entity to modify: "

        );


      if (per.Status != PromptStatus.OK)

        return;


      Transaction tr =

        db.TransactionManager.StartTransaction();

      using (tr)

      {

        Entity ent =

          (Entity)

            tr.GetObject(

              per.ObjectId,

              OpenMode.ForRead

            );


        ColorDialog cd = new ColorDialog();

        cd.Color = ent.Color;


        System.Windows.Forms.DialogResult dr;


        dr = cd.ShowDialog();

        if (dr != System.Windows.Forms.DialogResult.OK)

          return;


        LinetypeDialog ltd = new LinetypeDialog();

        ltd.Linetype = ent.LinetypeId;


        dr = ltd.ShowDialog();

        if (dr != System.Windows.Forms.DialogResult.OK)

          return;


        LineWeightDialog lwd = new LineWeightDialog();

        lwd.LineWeight = ent.LineWeight;


        dr = lwd.ShowDialog();

        if (dr != System.Windows.Forms.DialogResult.OK)

          return;


        ent.UpgradeOpen();

        if (ent.Color != cd.Color)

          ent.Color = cd.Color;

        if (ent.LinetypeId != ltd.Linetype)

          ent.LinetypeId = ltd.Linetype;

        if (ent.LineWeight != lwd.LineWeight)

          ent.LineWeight = lwd.LineWeight;


        tr.Commit();

      }

    }

  }

}

Notice that we "return" from the code in a number of places, should the user cancel one of the dialogs. This is perfectly safe, as we're "using" the transaction object, and once it leaves scope it will automatically be disposed (and therefore aborted).

Running the SPE command will display in sequence the dialogs shown in the previous post, and use the user's input to change the state of the selected entity.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e200e5502fde668833

Listed below are links to weblogs that reference Modifying the color, linetype and lineweight of an AutoCAD entity using standard dialogs from .NET:

blog comments powered by Disqus

Feed/Share

10 Random Posts