AddTransient won't draw image AutoCAD 2023

AddTransient won't draw image AutoCAD 2023

b.alikrnc
Observer Observer
295 Views
1 Reply
Message 1 of 2

AddTransient won't draw image AutoCAD 2023

b.alikrnc
Observer
Observer

Hi,

 

I want to draw a transient line by a specific distance when I hover over another line, but when I use the method ctm.AddTransient(line, AcGi.TransientDrawingMode.Main, 128, ints), it is not drawing any transient line. In another code I am using the method with block references as ctm.AddTransient(bref, AcGi.TransientDrawingMode.DirectShortTerm, 128, ints) and it works without problems.

 

Now I am working on a code which I changed some parts from another source in order to understand how I can achieve my goal.

 

Here is the full code for you to examine. I am a newbie so please be more specific about your suggestions and I will try to understand them as much as I can 😭.  Thanks for your help.

 

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System;
using AcGi = Autodesk.AutoCAD.GraphicsInterface;

public class PointMonitorTooltips

{

    [CommandMethod("SM")]

    public static void StartMonitor()

    {

        Editor ed =

          Application.DocumentManager.MdiActiveDocument.Editor;

        ed.PointMonitor +=

          new PointMonitorEventHandler(ed_PointMonitor);

    }



    [CommandMethod("XM")]

    public static void StopMonitor()

    {

        Editor ed =

          Application.DocumentManager.MdiActiveDocument.Editor;

        ed.TurnForcedPickOn();

        ed.PointMonitor -=

          new PointMonitorEventHandler(ed_PointMonitor);

    }



    static void ed_PointMonitor(object sender, PointMonitorEventArgs e)
    {
        Editor ed = (Editor)sender;

        AcGi.TransientManager ctm = AcGi.TransientManager.CurrentTransientManager;

        IntegerCollection ints = new IntegerCollection(new int[] { });

        try
        {
            FullSubentityPath[] paths = e.Context.GetPickedEntities();

            // Open a transaction to create the transient line
            using (Transaction tr = ed.Document.TransactionManager.StartTransaction())
            {
                foreach (FullSubentityPath path in paths)
                {
                    ObjectId[] ids = path.GetObjectIds();

                    if (ids.Length > 0)
                    {
                        ObjectId id = ids[ids.GetUpperBound(0)];

                        // Get the entity
                        Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                        Type objtype = ent.GetType();

                        // Create a transient line based on the entity's position and orientation
                        if (objtype == typeof(Line))
                        {

                            Line linecoor = (Line)ent.Clone();

                            Point3d start = linecoor.StartPoint;
                            start = start.Add(new Vector3d(5.0, 5.0, 0.0));

                            Point3d end = linecoor.EndPoint;
                            end = end.Add(new Vector3d(5.0, 5.0, 0.0));

                            Line line = new Line(start, end);

                            // Add the transient line
                            ed.WriteMessage("\nAdding transient line..."); // This part constantly sending messages 

                            ctm.AddTransient(line, AcGi.TransientDrawingMode.Main, 128, ints); // This part is not drawing anything
                        }
                    }
                }

                tr.Commit();
            }
        }

        catch
        {
            // Exception handling code here
        }

    }

}

 

0 Likes
296 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor

Your code is quite problematic: once you enables PointMonitor event handler, which could last entire running AutoCAD session, every time the mouse cursor hovers a Line entity with tiny bit of move (as long as the cursor pick box on top of the Line entity), the code would create a NEW LINE entity, which is never get disposed. So, you would end up with thousands of hundreds of non-db-residing Line objects in the memory of AutoCAD process.

 

You should limited the PointMonitor handler within a scope of a command, if the business requirements allow it. If you have to make the event handler in the session scope, you might want to consider to use a static, non-DB-residing Line entity instance and use Add/Update/EraseTransient() method accordingly.

 

That is, the first time the cursor hovers a Line entity, you create the static Line entity as drawable and call AddTransient(); when the cursor move off a Line (either hovers other type of entity, or hovers none), you call EraseTransient(). When the cousor hovers an Line entity again, you update the static line according to the Line the cursor hovers, and call UpdateTransient().

 

 

Norman Yuan

Drive CAD With Code

EESignature