Author Topic: A debugging exercise ...  (Read 4420 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
A debugging exercise ...
« on: May 26, 2009, 01:28:11 AM »

This is a programming exercise ... not optimised and not bomb proof.
.. and needs to have UCS translations added :)

I wanted to write a generic getObject Method to add to my Library
.. and also had a play with Debug.WriteLine(.. to the Output screen in VS2008

The core functional problem (from another forum) was
" to select a Line and Move its Endpoint"

Here's a Video ..




More to follow.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: A debugging exercise ...
« Reply #1 on: May 26, 2009, 01:38:34 AM »

This is the Output Pane and Locals Pane at the completion of the routine.
Note the Breakpoint at the last brace of the transaction statement.

T write the data to the output Pane ;

add a using statement
using System.Diagnostics;

then use the WriteLine Method of the Debug Class ;

            Debug.WriteLine("\nSelectLine_1 : -------------------------------");
            Debug.WriteLine("\nEntity.GetType() : " + pickedLine.GetType().Name);

etc

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: A debugging exercise ...
« Reply #2 on: May 26, 2009, 01:43:56 AM »
Then the Code

.. and a file attachment for members ..

Comments, Improvements, ideas appreciated ..

Code: [Select]
//CodeHimBelongaKdub ©  May2009

    public class MoveEndOfLine
    {
        ///-------------------------------------------------------------------
        ///
        [CommandMethod("SOL")]
        public void SelectLine_1()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

            Point3d pickPoint;
            ObjectId pickedID = SelectObject(
                "\nSelect Line entity near end to Relocate : ",
                typeof(Line), out pickPoint
            );

            if (pickedID.IsNull)
            {
                WinForms.MessageBox.Show(
                    "ooops !! \nCancelling Routine.",  "F.U.B.B.",
                    WinForms.MessageBoxButtons.OK,
                    WinForms.MessageBoxIcon.Hand
                );
                return;
            }
            Line pickedLine;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                pickedLine = tr.GetObject(pickedID, OpenMode.ForRead) as Line;
            }
            Debug.WriteLine("\nSelectLine_1 : -------------------------------");
            Debug.WriteLine("\nEntity.GetType() : " + pickedLine.GetType().Name);
            Debug.WriteLine("\npickedPoint() : " + pickPoint);
            Debug.WriteLine("\nstartPoint() : " + pickedLine.StartPoint);
            Debug.WriteLine("\nendPoint() : " + pickedLine.EndPoint);

            Point3d vertexPoint;
            Point3d basePoint;
            bool startPoint_IS_vertex = (
                pickPoint.DistanceTo(pickedLine.StartPoint)
                <= 
                pickPoint.DistanceTo(pickedLine.EndPoint)
            );
            if (startPoint_IS_vertex)
            {
                Debug.WriteLine("\nStartPoint to PickPoint : "
                    + pickPoint.DistanceTo(pickedLine.StartPoint));
                vertexPoint = pickedLine.StartPoint;
                basePoint = pickedLine.EndPoint;               
            }
            else
            {
                Debug.WriteLine("\nEndPoint to PickPoint : "
                    + pickPoint.DistanceTo(pickedLine.EndPoint));
                vertexPoint = pickedLine.EndPoint;
                basePoint = pickedLine.StartPoint;
            }
            //-----------------------------------------------------------------
           
            PromptPointOptions ppo = new PromptPointOptions("\nGet New EndPoint: ");
            ppo.UseBasePoint = true;
            ppo.UseDashedLine = true;
            ppo.BasePoint = basePoint;

            PromptPointResult ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nSelection is null ... Cancelling Routine.");
                return;               
            }
            Point3d p = ppr.Value;

            Debug.WriteLine("\nNew Vertex Location : " + p);
           
            // Change property of appropriate Line End to be the selected Point.
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                pickedLine = tr.GetObject(pickedID, OpenMode.ForWrite) as Line;
                if (startPoint_IS_vertex)
                {
                    pickedLine.StartPoint = p;
                }
                else
                {
                    pickedLine.EndPoint = p;
                }
                tr.Commit();
               
            }
 
        }

Code: [Select]
//CodeHimBelongaKdub ©  May2009

        static ObjectId SelectObject(string prompt,
            System.Type objectClass,
            out Point3d pickPoint)
        {
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            using (Transaction tr = ed.Document.Database.TransactionManager.StartTransaction())
            {
                while (true)
                {
                    PromptEntityResult res = ed.GetEntity(prompt);
                    if (res.Status != PromptStatus.OK)
                        return ObjectId.Null;
                    DBObject dbObj = tr.GetObject(res.ObjectId, OpenMode.ForRead);
                    if (objectClass.IsAssignableFrom(dbObj.GetType()))
                    {
                        pickPoint = res.PickedPoint;
                        return res.ObjectId;
                    }
                    ed.WriteMessage("\nInvalid selection, {0} entity expected",
                        RXClass.GetClass(objectClass).DxfName);
                }
            }
        }
« Last Edit: May 26, 2009, 01:54:39 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: A debugging exercise ...
« Reply #3 on: May 26, 2009, 09:56:31 AM »
Kerry,

What's your reasoning behind using IsAssignableFrom() ?

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: A debugging exercise ...
« Reply #4 on: May 26, 2009, 08:50:28 PM »

Hi Glenn

I stole that idea from Tony

.. lets me do this,
Code: [Select]
        ///----------------------------------------------------------
        ///
        [CommandMethod("S1")]
        public static void Select_Curved_Thingy()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

            Point3d pickPoint;
            ObjectId pickedID = SelectObject(
                "\nSelect Curved Thingy : ",
                typeof(Curve), out pickPoint
            );           
            if (pickedID.IsNull)
            {               
                return;
            }
            Curve PickedThingy;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PickedThingy = tr.GetObject(pickedID, OpenMode.ForRead) as Curve;
            }
            ed.WriteMessage("\nSelectLine_1 : -------------------------------");
            ed.WriteMessage("\nGetType() : " + PickedThingy.GetType().Name);
            ed.WriteMessage("\npickedPoint() : " + pickPoint);
        }
        ///----------------------------------------------------------
        ///


which selects anything derived from the defined class



kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: A debugging exercise ...
« Reply #5 on: May 27, 2009, 06:04:51 AM »
OK, thanks for that Kerry. I would have used PromptEntityOptions.AddAllowedClass(...), so I was just curious as to the reasoning.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: A debugging exercise ...
« Reply #6 on: May 27, 2009, 06:56:58 AM »

Glenn,
yes, that's probably more intuitive, and equally effective at first testing :)

Code: [Select]

        ///----------------------------------------------------------
        ///
        //CodeHimBelongaKdub ©  May2009
        static ObjectId SelectObject(string prompt, System.Type objectClass, out Point3d pickPoint)
        {
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            using (Transaction tr = ed.Document.Database.TransactionManager.StartTransaction())
            {
                PromptEntityOptions peo = new PromptEntityOptions(prompt);               
                peo.SetRejectMessage("\nInvalid selection, "
                    + objectClass.Name
                    + " entity expected");
                peo.AddAllowedClass(objectClass, false);

                PromptEntityResult res = ed.GetEntity(peo);
                if (res.Status != PromptStatus.OK)
                {
                    ed.WriteMessage("\nNothing Selected");
                    return ObjectId.Null;
                }
                // else all is good :)
                DBObject dbObj = tr.GetObject(res.ObjectId, OpenMode.ForRead);
                pickPoint = res.PickedPoint;
                return res.ObjectId;
            }
        }
       

        ///----------------------------------------------------------
        ///
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: A debugging exercise ...
« Reply #7 on: June 01, 2009, 06:29:04 AM »
Thought you might like it ;)

Kerry, what IDE enhancement(s) are you running? ie. your piccy shows vertical lines connecting braces, lines defining the scope of 'if' blocks etc.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: A debugging exercise ...
« Reply #8 on: June 01, 2009, 08:45:10 AM »

Glenn,

That would be CodeRush and Refactor from devexpress
.. actually the light free version
http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/

Was running WholeTomato Visual AssistX .. but took it off rather than upgrade.

regards
kdub
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: A debugging exercise ...
« Reply #9 on: June 01, 2009, 08:54:08 AM »
I suspected it was either of those 2 - thanks for the confirmation.

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: A debugging exercise ...
« Reply #10 on: June 01, 2009, 08:57:20 AM »
just to bring things up to date ..
.. and to further show how badly I translate Chinese
The original poster wasn't trying to move the endpoint
he was trying to control the DrawOrder of objects   :oops:

Bill Wu from AutoDesk China posted something on his blog late last year
http://www.cnblogs.com/wf225/archive/2008/11/14/1333852.html

// // Get the DrawOrderTable
    //DrawOrderTable orderTable = tr.GetObject(btr.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable;
  // < .. big snip ..>
   // orderTable.MoveAbove(ids, id2); //It's OK.
   // orderTable.MoveBelow(ids, id2); //It's OK.
   // orderTable.MoveToBottom(ids); //It's OK.
   // orderTable.MoveToTop(ids); //It's OK.


The blog is well worth a look.

... and of course, Kean has a few references on his Blog :)
« Last Edit: June 01, 2009, 09:04:53 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.