Author Topic: Dispose of entities in a jig??  (Read 2576 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8858
  • AKA Daniel
Dispose of entities in a jig??
« on: April 11, 2008, 01:36:51 PM »
Should we be calling dispose on entities created but NOT added to the database? Does it really matter?  :mrgreen:
here is my fun with jigs

Code: [Select]
/*
 * Daniel Marcotte 2008
 * Based on Bobby’s code;
 * http://www.theswamp.org/index.php?topic=8195.msg104906#msg104906
 */


#region #Using delarations
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Threading;
using System.Globalization;
using System.Reflection;
using System.Drawing.Imaging;

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 AcExtensions; mine

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;
#endregion

[assembly: CommandClass(typeof(ExecMethod.Commands))]
namespace ExecMethod
{
  public class Commands
  {
    [CommandMethod("doit")]
    public void doit()
    {
      Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
      try
      { //Dispose the whole jig : |
        using (JigExample jig = new JigExample("\nPick Point to Place Cabinet", 10))
        {
          Point3d pt = jig.StartJig();
          ed.WriteMessage(pt.ToString());
        }
      }
      catch
      {
        ed.WriteMessage("Operation Canceled");
      }
    }
  }

  //make a sample entity
  public class MyEntity : Polyline
  {
    // Fields
    internal Point2d m_pt1;
    internal Point2d m_pt2;
    internal Point2d m_pt3;
    internal Point2d m_pt4;

    // Constructors
    public MyEntity()
      : base(4)
    {
      this.m_pt1 = new Point2d(0.0, 0.0);
      this.m_pt2 = new Point2d(0.0, 100.0);
      this.m_pt3 = new Point2d(100.0, 100.0);
      this.m_pt4 = new Point2d(100.0, 0.0);
      this.Doit();
    }

    // Methods
    internal void Doit()
    {
      base.AddVertexAt(0, this.m_pt1, 0.0, 0.0, 0.0);
      base.AddVertexAt(1, this.m_pt2, 0.0, 0.0, 0.0);
      base.AddVertexAt(2, this.m_pt3, 0.0, 0.0, 0.0);
      base.AddVertexAt(3, this.m_pt4, 0.0, 0.0, 0.0);
      base.Closed = true;
    }

    public void DrawFrom(Vector2d from)
    {
      base.SetPointAt(0, this.m_pt1.Add(from));
      base.SetPointAt(1, this.m_pt2.Add(from));
      base.SetPointAt(2, this.m_pt3.Add(from));
      base.SetPointAt(3, this.m_pt4.Add(from));
    }

    public void DrawFrom(Point2d from)
    {
      this.DrawFrom(new Vector2d(from.ToArray()));
    }

    public void DrawFrom(Point3d from)
    {
      this.DrawFrom(new Vector2d(from.X, from.Y));
    }
  }

  //and the jig
  public class JigExample : DrawJig, IDisposable
  {
    // Fields
    private Point3d currentCursorPosition;
    private bool disposed;
    private MyEntity entityToDrag;
    private string m_message;
    private Point3d previousCursorPosition;

    // Constructors
    private JigExample() { }

    public JigExample(string message, short color /* add stuff */)
    {
      this.m_message = message;
      this.entityToDrag = new MyEntity();
      this.entityToDrag.Color = Color.FromColorIndex(ColorMethod.ByAci, color);
      this.previousCursorPosition = new Point3d(0.0, 0.0, 0.0);
    }

    // Methods
    private bool CursorHasMoved()
    {
      return !(this.currentCursorPosition == this.previousCursorPosition);
    }

    public void Dispose()
    {
      this.Dispose(true);
      GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
      if (!this.disposed)
      {
        if (disposing)
        {
          this.entityToDrag.Dispose();
        }
        this.disposed = true;
      }
    }

    protected override SamplerStatus Sampler(JigPrompts prompts)
    {
      PromptPointResult userFeedback = prompts.AcquirePoint(this.m_message);
      this.currentCursorPosition = userFeedback.Value;
      if (this.CursorHasMoved())
      {
        this.entityToDrag.DrawFrom(this.currentCursorPosition);
        this.previousCursorPosition = this.currentCursorPosition;
        return SamplerStatus.OK;
      }
      return SamplerStatus.NoChange;
    }

    public Point3d StartJig()
    {
     
      PromptResult stat =
        AcAp.Application.DocumentManager.MdiActiveDocument.Editor.Drag(this);

      if (stat.Status != PromptStatus.OK)
      {
        throw new Autodesk.AutoCAD.Runtime.Exception();
      }
      return this.currentCursorPosition;
    }

    protected override bool WorldDraw(WorldDraw draw)
    {
      draw.Geometry.Draw(this.entityToDrag);
      return true;
    }
  }
}

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8858
  • AKA Daniel
Re: Dispose of entities in a jig??
« Reply #1 on: April 11, 2008, 02:13:03 PM »
Attached is an AVI of this in use, I hope it works

Glenn R

  • Guest
Re: Dispose of entities in a jig??
« Reply #2 on: April 11, 2008, 02:58:55 PM »
Should we be calling dispose on entities created but NOT added to the database? Does it really matter?  :mrgreen:

Shame on you Dan  :wink: . You should know from ARX, that whatever you create and DON'T add to the database, you destroy...period.
The same holds true for the managed API's (C# etc.) as they are using native implemented 'wrappers'.

So, at the end of the day, be a good binary citizen and clean up. Anything that's added to the dbase is Acad's responsibility.

These are the rules I follow.

Cheers,
Glenn.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8858
  • AKA Daniel
Re: Dispose of entities in a jig??
« Reply #3 on: April 12, 2008, 12:29:06 AM »

Hehe Thanks for the confirmation. 

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Dispose of entities in a jig??
« Reply #4 on: April 12, 2008, 11:57:31 AM »
The video was good.