.NET

Reply
Active Contributor
pva75
Posts: 48
Registered: ‎07-22-2012
Message 1 of 4 (395 Views)
Accepted Solution

How to change object from context menu?

395 Views, 3 Replies
08-18-2012 09:14 AM

According to this http://adndevblog.typepad.com/autocad/2012/05/object-specific-context-menu-using-net.html article I tried to add context menu to my object:

 

ContextMenuExtension nodeContextMenu = new ContextMenuExtension();

.... 

Application.AddObjectContextMenuExtension(RXClass.GetClass(typeof(DBPoint)), nodeContextMenu);

 

Next, I found selected object:

 

private static ObjectId GetSelectedObject()

{

  Document doc = Application.DocumentManager.MdiActiveDocument;

 

  Editor ed = doc.Editor;

 

  PromptSelectionResultacSSPrompt = ed.SelectImplied();

 

  if (acSSPrompt.Status != PromptStatus.OK)

         return  ObjectId.Null;

 

  SelectionSetset = acSSPrompt.Value;

 

  ObjectId[] ids = set.GetObjectIds();

 

  if(ids.Length != 1)

     returnObjectId.Null;

 

  returnids[0];

}

 

Ok, I found it and tried to change properties of selected object:

 

using (Transactiontx = db.TransactionManager.StartTransaction())

{

  DBPoint point = tx.GetObject(selectedObject, OpenMode.ForWrite) asDBPoint;

 

At this line Autocad has been crashed.

 

This line works if I open object ForRead, but I can't open it ForWrite or call

UpgradeOpen.

 

Is it possible to change object from context menu?

 

Thanks,

Pavel.

Hi,

 

By my side, I define two separated classes:

- a class for the context menu with two static methods: Attach() and Detach() to add or remove the context menu and the MenuItem.Click() event handler which calls a command

- a class implementing IextensionApplication to add the context menu at startup in which the command is defined. The command uses the UsePickSet CommandFlag and some PromptSelectionSettings so that it works with a single entity and can be used from the command line or from the context menu.

 

Here's a little sample:

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace PointEdit
{
public class Commands : IExtensionApplication
{
public void Initialize()
{
ContextMenu.Attach();
}
public void Terminate()
{
ContextMenu.Detach();
}
[CommandMethod("PointEdit", CommandFlags.UsePickSet)]
public void PointEdit()
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
TypedValue[] filter = new TypedValue[1] { new TypedValue(0, "POINT") };
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.SingleOnly = true;
pso.SinglePickInSpace = true;
pso.SelectEverythingInAperture = true;
pso.RejectObjectsOnLockedLayers = true;
PromptSelectionResult psr;
do
{
psr = ed.GetSelection(pso, new SelectionFilter(filter));
if (psr.Status != PromptStatus.OK) return;
} while (psr.Value.Count > 1);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBPoint pt = (DBPoint)tr.GetObject(psr.Value.GetObjectIds()[0], OpenMode.ForWrite);
pt.Color = Color.FromColorIndex(ColorMethod.ByAci, 30);
tr.Commit();
}
}
}
class ContextMenu
{
private static ContextMenuExtension cme;
internal static void Attach()
{
cme = new ContextMenuExtension();
MenuItem mi = new MenuItem("PointEdit");
mi.Click += new EventHandler(Edit);
cme.MenuItems.Add(mi);
RXClass rxc = Entity.GetClass(typeof(DBPoint));
AcAp.AddObjectContextMenuExtension(rxc, cme);
}
internal static void Detach()
{
RXClass rxc = Entity.GetClass(typeof(DBPoint));
AcAp.RemoveObjectContextMenuExtension(rxc, cme);
}
private static void Edit(object sender, EventArgs e)
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
doc.SendStringToExecute("POINTEDIT ", true, false, true);
}
}
}

 

*Expert Elite*
_gile
Posts: 2,105
Registered: ‎04-29-2006
Message 2 of 4 (372 Views)

Re : How to change object from context menu?

08-19-2012 06:41 AM in reply to: pva75

Hi,

 

By my side, I define two separated classes:

- a class for the context menu with two static methods: Attach() and Detach() to add or remove the context menu and the MenuItem.Click() event handler which calls a command

- a class implementing IextensionApplication to add the context menu at startup in which the command is defined. The command uses the UsePickSet CommandFlag and some PromptSelectionSettings so that it works with a single entity and can be used from the command line or from the context menu.

 

Here's a little sample:

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace PointEdit
{
    public class Commands : IExtensionApplication
    {
        public void Initialize()
        {
            ContextMenu.Attach();
        }

        public void Terminate()
        {
            ContextMenu.Detach();
        }

        [CommandMethod("PointEdit", CommandFlags.UsePickSet)]
        public void PointEdit()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            TypedValue[] filter = new TypedValue[1] { new TypedValue(0, "POINT") };
            PromptSelectionOptions pso = new PromptSelectionOptions();
            pso.SingleOnly = true;
            pso.SinglePickInSpace = true;
            pso.SelectEverythingInAperture = true;
            pso.RejectObjectsOnLockedLayers = true;
            PromptSelectionResult psr;
            do
            {
                psr = ed.GetSelection(pso, new SelectionFilter(filter));
                if (psr.Status != PromptStatus.OK) return;
            } while (psr.Value.Count > 1);
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBPoint pt = (DBPoint)tr.GetObject(psr.Value.GetObjectIds()[0], OpenMode.ForWrite);
                pt.Color = Color.FromColorIndex(ColorMethod.ByAci, 30);
                tr.Commit();
            }
        }
    }

    class ContextMenu
    {
        private static ContextMenuExtension cme;

        internal static void Attach()
        {
            cme = new ContextMenuExtension();
            MenuItem mi = new MenuItem("PointEdit");
            mi.Click += new EventHandler(Edit);
            cme.MenuItems.Add(mi);
            RXClass rxc = Entity.GetClass(typeof(DBPoint));
            AcAp.AddObjectContextMenuExtension(rxc, cme);
        }

        internal static void Detach()
        {
            RXClass rxc = Entity.GetClass(typeof(DBPoint));
            AcAp.RemoveObjectContextMenuExtension(rxc, cme);
        }

        private static void Edit(object sender, EventArgs e)
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            doc.SendStringToExecute("POINTEDIT ", true, false, true);
        }
    }
}

 

Gilles Chanteau
Active Contributor
pva75
Posts: 48
Registered: ‎07-22-2012
Message 3 of 4 (358 Views)

Re : How to change object from context menu?

08-19-2012 10:00 AM in reply to: _gile

Thanks!

 

I did as you said and it woks fine (also I send paramaters by doc.UserData).

 

Pavel.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 4 (351 Views)

Re : How to change object from context menu?

08-19-2012 11:32 AM in reply to: pva75

Here is one more toy to play with your points:

 

        using Autodesk.AutoCAD.Runtime;
        using Autodesk.AutoCAD.ApplicationServices;
        using Autodesk.AutoCAD.DatabaseServices;
        using Autodesk.AutoCAD.Geometry;
        using Autodesk.AutoCAD.EditorInput;
        using Autodesk.AutoCAD.Interop.Common;
        using Autodesk.AutoCAD.Interop;
        //____________________________________________________//

        private static ObjectId GetSelectedObject()
        {

            Document doc = Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            PromptSelectionResult acSSPrompt = ed.SelectImplied();

            if (acSSPrompt.Status != PromptStatus.OK)

                return ObjectId.Null;

            SelectionSet set = acSSPrompt.Value;

            ObjectId[] ids = set.GetObjectIds();

            if (ids.Length != 1)

                return ObjectId.Null;

            return ids[0];

        }

        [CommandMethod("PTE", CommandFlags.UsePickSet)]
        public static void EditPoint()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            ObjectId id = ObjectId.Null;
            using (Transaction tx = db.TransactionManager.StartTransaction())
            {
                 id = GetSelectedObject();
                 if (id == ObjectId.Null)
                 {
                     Application.ShowAlertDialog("Select object first!");
                     return;

                 }
                Entity ent = tx.GetObject(id, OpenMode.ForRead, false) as Entity;
                // check if DBPoint is selected
                if (!id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(DBPoint))))
                {
                    Application.ShowAlertDialog("You have to select a Point object only!");
                    return;
                }

                DBPoint point = ent as DBPoint;
                point.UpgradeOpen();
                // do you mojo here
                point.ColorIndex = 121;
                tx.Commit();
            }
        }
        //  written by Alexander Rivilis
        [CommandMethod("MyTool")]
        static public void AddToolbar()
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            try
            {
                AcadDocument acdoc = doc.AcadDocument as AcadDocument;

                AcadApplication acad = (AcadApplication)acdoc.Application;
                IAcadMenuGroups pMnuGrps = acad.MenuGroups;
                int nums = pMnuGrps.Count;
                IAcadMenuGroup pMnuGrp = pMnuGrps.Item(nums - 1);

                AcadToolbars pTlbrs = pMnuGrp.Toolbars;
                IAcadToolbar pTlbr = pTlbrs.Add("Modify toolbar");

                IAcadToolbarItem pTlbrItem = pTlbr.AddToolbarButton(
                0, // Item number
                "Mocoro", // Item name
                "Move Copy Rotate", // Item help string
                "\x1b\x1b_.MOCORO " // Item macro string
                );
                pTlbrItem.SetBitmaps("et_MOCORO16.bmp", "et_MOCORO24.bmp");

                pTlbrItem = pTlbr.AddToolbarButton(
                1, // Item number
                "CopyM", // Item name
                "Copy Multiple", // Item help string
                "\x1b\x1b_.COPY " // Item macro string
                );
                pTlbrItem.SetBitmaps("et_COPYM16.bmp", "et_COPYM24.bmp");

                pTlbrItem = pTlbr.AddToolbarButton(
               2, // Item number
               "Stretch", // Item name
               "Stretch", // Item help string
                 "\x1b\x1b_.STRETCH " // Item macro string    
               );
                pTlbrItem.SetBitmaps("et_MSTRCH16.bmp", "et_MSTRCH24.bmp");
                pTlbrItem = pTlbr.AddToolbarButton(
              2, // Item number
              "Point Edit", // Item name
              "Edit point color", // Item help string
              "\x1b\x1b_.PTE " // Item macro string    // <--- "PTE" is your command name within the same project   --->
              );
                pTlbrItem.SetBitmaps("et_SHWURL16.bmp", "et_SHWURL24.bmp");

                // ad separators around the button
                IAcadToolbarItem pTlbrSep1 = pTlbr.AddSeparator(pTlbr.Count - 2);
                IAcadToolbarItem pTlbrSep2 = pTlbr.AddSeparator(pTlbr.Count - 1);
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage(ex.Message);
            }
        }

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.