Author Topic: drawing temp graphics  (Read 9376 times)

0 Members and 2 Guests are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8796
  • AKA Daniel
Re: drawing temp graphics
« Reply #15 on: November 04, 2009, 07:11:58 AM »
I couldn't get the text to justify either  :x

Code: [Select]
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.LayerManager;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;

using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcRx = Autodesk.AutoCAD.Runtime;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcWd = Autodesk.AutoCAD.Windows;

[assembly: CommandClass(typeof(ExecMethod.Commands))]
namespace ExecMethod
{

 public static class Commands
 {

  static TransientEntityCollection _drawn = new TransientEntityCollection();

  [CommandMethod("doit")]
  static public void doit()
  {
   AcDb.TransactionManager tam =
    HostApplicationServices.WorkingDatabase.TransactionManager;

   Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;


   TypedValue[] values = new TypedValue[]
                     {
                        new TypedValue((short)DxfCode.Start, "POINT") ,
                     };

   SelectionFilter filter = new SelectionFilter(values);
   PromptSelectionOptions selopts = new PromptSelectionOptions();
   selopts.MessageForAdding = "Select";
   selopts.MessageForRemoval = "DeSelect";
   selopts.AllowDuplicates = false;
   PromptSelectionResult result = ed.GetSelection(selopts, filter);

   if (result.Status == PromptStatus.OK)
   {
    ObjectId[] idarray = result.Value.GetObjectIds();


    Transaction tr = tam.StartTransaction();
    try
    {
     foreach (ObjectId id1 in idarray)
     {
      DBPoint ent = tr.GetObject(id1, OpenMode.ForRead, true) as DBPoint;
      DBText text = new DBText();
      text.TextString = ent.Position.ToString();
      text.Position = ent.Position;
      //text.HorizontalMode = TextHorizontalMode.TextMid;
      //text.VerticalMode = TextVerticalMode.TextVerticalMid;
      //text.AlignmentPoint = ent.Position;
      _drawn.Add(new TransientEntity(text));
     }
    }
    catch (System.Exception ex)
    {
     ed.WriteMessage(ex.Message);
    }
    finally
    {
     tr.Dispose();
    }
   }

   TransientManager tm = TransientManager.CurrentTransientManager;
   _drawn.ForEach(Item =>
      tm.AddTransient(Item.Entity,
        TransientDrawingMode.DirectShortTerm, 0, Item.ViewportNumbers));
  }

  //could be added to a reactor??
  [CommandMethod("updoit")]
  static public void updoit()
  {
   TransientManager tm = TransientManager.CurrentTransientManager;
   _drawn.ForEach(Item => tm.UpdateTransient(Item.Entity,Item.ViewportNumbers));
  }

  [CommandMethod("undoit")]
  static public void undoit()
  {
   _drawn.Clean(TransientManager.CurrentTransientManager);
  }
 }
}



jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Re: drawing temp graphics
« Reply #16 on: November 04, 2009, 12:41:17 PM »
wow, thanks for your testing on this.
Just seeing how you slick out the tracking collection is helpful.
I put that finally clause in there to avoid a problem I ran into.
It was not erasing the last entity when I later ran the erase code.
I realized disposing of the entity used to derive the drawable would cause the last item in the tracking collection to not work.
But if i set the ent to a new ent, disposing of it was ok.  I had thought the tracking collection had copies of objects so there would be no connection, but there was.  It was not easy to catch.

I agree on the aliases and point3d usage.  The array of doubles was a choice that I could switch now, long story on that.
I can synthesize the alignment point shift since it seems like a bug is going on. thx
James Maeding

SEANT

  • Bull Frog
  • Posts: 345
Re: drawing temp graphics
« Reply #17 on: November 07, 2009, 04:29:47 AM »
This Topic/Thread is of a style that appears regularly; one in which I lack enough background to offer any useful comments, but one, when its relevance inevitably arises, I’ll be happy is available.   

Let me thank everyone now while the thread is still fresh.     
Sean Tessier
AutoCAD 2016 Mechanical

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Re: drawing temp graphics
« Reply #18 on: November 23, 2009, 05:24:50 PM »
I finally got some help on the dbtext justify problem.
It seems we should not use the Justify property, but the TextHorizontalMode and TextVerticalMode instead.
Also, use Position prop for items "Base Left" justified, and AlignmentPoint for other justify styles.
Lastly, run AdjustAlignment method on all styles after other props are set.

It seems the AdjustAlignment method is needed because we are dealing with a dbtext object that never gets put in the database.  Normally it would get adjusted when the transaction is committed, but we must force it since no transaction involved.
James Maeding

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Re: drawing temp graphics
« Reply #19 on: December 01, 2009, 02:21:52 PM »
Daniel,
If you have a second, can you explain the porpose of the isCleaned property of the transient ent class?
It seems to me the mere presence of the item in the collection implies its not cleaned.
Why not remove from the collection when cleaned - or actually delete the whole collection once we clean it out?
I wondering if there is some suttle issue i am missing invoving the disposal of the entities here.
thx
James Maeding

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8796
  • AKA Daniel
Re: drawing temp graphics
« Reply #20 on: December 08, 2009, 11:01:47 PM »
better?

Code: [Select]
using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Geometry;

namespace ExecMethod
{
 public class TransientEntityCollection : List<TransientEntity>, IDisposable
 {
 
  public void AddTransients(TransientManager manager,
                            TransientDrawingMode drawingMode,
                            int subDrawingMode)
  {
   this.ForEach(Item =>
    Item.AddTransient(manager,drawingMode,subDrawingMode));
  }

  public void AddTransients(TransientManager manager,
                            TransientDrawingMode drawingMode,
                            int subDrawingMode,
                            IntegerCollection viewportNumbers)
  {
   this.ForEach(Item =>
    Item.AddTransient(manager, drawingMode, subDrawingMode, viewportNumbers));
  }

  public void Dispose()
  {
   this.ForEach(Item => Item.Dispose());
   this.Clear();
  }

  public void Dispose(int item)
  {
   this[item].Dispose();
   this.RemoveAt(item);
  }

  public void EraseTransients(TransientManager manager)
  {
   this.ForEach(Item =>
    Item.EraseTransient(manager));
  }
  public void EraseTransients(TransientManager manager, bool dispose)
  {
   this.ForEach(Item =>
    Item.EraseTransient(manager,dispose));
  }
 }

 public class TransientEntity : IDisposable
 {
  // Fields
  protected bool m_isTransient;
  protected Entity m_entity;
  protected IntegerCollection m_viewportNumbers;

  // Methods
  private TransientEntity()
  {
  }

  public TransientEntity(Entity entity)
  {
   if (entity == null)
    throw new ArgumentNullException();
   this.m_viewportNumbers = new IntegerCollection();
   this.m_entity = entity;
   m_isTransient = false;
  }

  public TransientEntity(Entity entity, IntegerCollection viewportNumbers)
  {
   if (entity == null)
    throw new ArgumentNullException();
   this.m_viewportNumbers = viewportNumbers;
   this.m_entity = entity;
   m_isTransient = false;
  }

  public virtual void AddTransient(TransientManager manager,
                                   TransientDrawingMode drawingMode,
                                   int subDrawingMode)
  {
   if (!this.m_entity.IsDisposed)
   {
    manager.AddTransient(this.m_entity, drawingMode,
                         subDrawingMode, this.m_viewportNumbers);
    m_isTransient = true;
   }
  }

  public virtual void AddTransient(TransientManager manager,
                                   TransientDrawingMode drawingMode,
                                   int subDrawingMode,
                                   IntegerCollection viewportNumbers)
  {
   if (!this.m_entity.IsDisposed)
   {
    manager.AddTransient(this.m_entity, drawingMode,
                         subDrawingMode, viewportNumbers);
    m_isTransient = true;
   }
  }

  public void Dispose()
  {
   this.m_entity.Dispose();
  }

  public virtual void EraseTransient(TransientManager manager)
  {
   manager.EraseTransient(this.m_entity, this.m_viewportNumbers);
   m_isTransient = false;
  }

  public virtual void EraseTransient(TransientManager manager, bool dispose)
  {
   this.EraseTransient(manager);
   if (dispose)
    this.m_entity.Dispose();
  }

  // Properties
  public Entity Entity
  {
   get
   {
    return this.m_entity;
   }
  }

  public bool IsDisposed
  {
   get
   {
    return this.m_entity.IsDisposed;
   }
  }

  public bool IsTransient
  {
   get
   {
    return this.m_isTransient;
   }
   set
   {
    this.m_isTransient = value;
   }
  }

  public bool IsResident
  {
   get
   {
    if (this.m_entity == null)
     return false;
    else if (this.IsDisposed)
     return false;
    return this.m_entity.ObjectId.IsResident;
   }
  }

  public ObjectId ObjectId
  {
   get
   {
    if (this.m_entity == null)
     return ObjectId.Null;
    else if (this.IsDisposed)
     return ObjectId.Null;
    return this.m_entity.ObjectId;
   }
  }

  public IntegerCollection ViewportNumbers
  {
   get
   {
    return this.m_viewportNumbers;
   }
   set
   {
    this.m_viewportNumbers = value;
   }
  }
 }
}
« Last Edit: December 10, 2009, 08:05:09 AM by Daniel »

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Re: drawing temp graphics
« Reply #21 on: December 09, 2009, 01:22:23 PM »
I see, you are keeping erased items in the collection, so need the flag if shown or not.
That purpose had not occured to me as I remove all items in my erase method of the collection.
Your way is pretty interesting, as I could maintain sets of entities in various collections, then display or not display items as the user toggles.
I am doing road and pipeline design progs, so I show all kinds of info in plan.  Some changes a lot as the user modifies the profile, and some rarely changes like stationing.

I am kind of not getting the isdisposed property though.
What is the real use of an object that has had dispose called on it?
How can an object be in scope after its been disposed?
I would think you get a crash from doing anything at all with it, even accessing properties.
I know this is a broader .net issue beyond acad entities, I need to get a handle on it.
James Maeding

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8796
  • AKA Daniel
Re: drawing temp graphics
« Reply #22 on: December 10, 2009, 08:26:51 AM »
I am kind of not getting the isdisposed property though.
What is the real use of an object that has had dispose called on it?
How can an object be in scope after its been disposed?
I would think you get a crash from doing anything at all with it, even accessing properties.
I know this is a broader .net issue beyond acad entities, I need to get a handle on it.


When you create a new Entity, your allocating memory in both the unmanaged and managed heap.  Typically calling dispose cleans up unmanaged memory while the remaining managed memory is cleaned up by the garbage collector. The IsDisposed property is in the managed heap. You would use the IsDisposed test to avoid an access violation.

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Re: drawing temp graphics
« Reply #23 on: December 10, 2009, 12:50:55 PM »
oh, thx for the schooling, I'm just learning the under the hood stuff  :-)
James Maeding