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.

13 responses to “Modifying the color, linetype and lineweight of an AutoCAD entity using standard dialogs from .NET”

  1. is there an exe that will set up vs2008 for AutoCAD apps?

  2. Richard -

    What are you trying to do? Managed (VB/C#) development or narice C++ (ObjectARX) development? And with which Autodesk products/versions?

    Kean

  3. Kean Walmsley Avatar

    AcDbDatabase:celweight() or Database.Celweight should do it.

    Kean

  4. Kean Walmsley Avatar

    Do you expect something different - e.g. have you set the "current lineweight" in that drawing as something other than the default (which is to have no lineweight applied)?

    Kean

  5. Kean Walmsley Avatar

    OK, maybe I'm getting things mixed up. Do you want the "current" lineweight or the "default" one?

    I've been assuming you wanted the current - which is used for new objects - but perhaps you want the default. What are you expecting to use it for?

    BTW - this is too long a thread for this blog. Please post your questions to the discussion group in future unless specifically related to a post...

    Kean

  6. Kean Walmsley Avatar

    Hi HK,

    It turns out that LWDEFAULT isn't a pre-drawing setting - it's stored in the Registry:

    HKEY_CURRENT_USERSoftwareAutodeskAutoCADR20.0ACAD-E001:409Profiles<<unnamed profile="">>GeneralLWDEFAULT

    I've copied a piece of the online documentation for the setting, too - that's a good place to check on these variables and whether they're per drawing or not.

    I won't delete the question, but I appreciate the consideration. Here's the discussion group link for future questions. I'd tend to go with the .NET forum, but it depends on the specific question:

    forums.autodesk.com/

    Regards,

    Kean

  7. I tried to assign a linetype to an entity. But in some cases it fails. What could be the reason?[cases are given below] Kindly clarify.

    ent.Linetype = "CONTINUOUS" [it works]
    ent.Linetype = "DIVIDE" [It fails with a error message 'eKeyNotFound']

    1. Kean Walmsley Avatar

      You need to make sure the linetype is loaded first (i.e. that there's a record with that name in the LineTypeTable).

      Your question isn't related to the code in this post. Please post future such questions to the AutoCAD .NET Discussion Group.

      Kean

Leave a Reply to Dharma Cancel reply

Your email address will not be published. Required fields are marked *