Author Topic: Lab 02 Reworked ..  (Read 6346 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Lab 02 Reworked ..
« on: December 02, 2005, 11:29:52 PM »
In line with this :   Topic: Lab 01 Reworked ..

The Lab 02 plus a bit.
Any Comments ? ?
Code: [Select]
#region System using declarations
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;

using System.Text;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.ComponentModel;
using System.Text.RegularExpressions;
#endregion

#region AutoCAD Managed Wrappers using declarations
//// Assembly acdbmgd .. ObjectDBX.NET Managed Wrapper
//// Assembly acmgd .. Autocad.NET Managed Wrapper
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;
#endregion

#region Alias using declarations
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
#endregion

[assembly: ExtensionApplication(typeof(ClassLibraryLab02.kwbLab02StartApp))]
[assembly: CommandClass(typeof(ClassLibraryLab02.Commands))]

namespace ClassLibraryLab02
{
    #region CommandClass
    //-------------------------------------------------------------------------------
    public class
    Commands
    {
        [CommandMethod("Lab02Help", CommandFlags.Modal)]
        static public void
        Lab02Help()
        {
            Editor
               ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

            ed.WriteMessage("\n  Testing Assembly kwbLab02 [kwb v:20051203]");
            ed.WriteMessage("\n  Commands : SelectPoint , GetDistance, GetEntity");
        }
        //----------------------------
        [CommandMethod("SelectPoint", CommandFlags.Modal)]
        static public void
        Lab02SelectPoint()
        {
            Class1 tester = new Class1();
            tester.SelectPoint();
        }
        ////----------------------------
        [CommandMethod("GetDistance", CommandFlags.Modal)]
        static public void
        Lab02GetDistance()
        {
            Class1 tester = new Class1();
            //call a function of the object.
            tester.GetDistance();
        }
        ////----------------------------
        [CommandMethod("GetEntity", CommandFlags.Modal)]
        static public void
        Lab02GetEntity()
        {
            Class1 tester = new Class1();
            //call a function of the object.
            tester.GetEntity();
        }
    }
    #endregion




    #region Class1
    //-------------------------------------------------------------------------------
    public class Class1
    {
        //----------------------------
        public void
        SelectPoint()
        {
            Editor
                 ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            PromptPointOptions
                pointOpt = new PromptPointOptions("\nSelect a point");
            PromptPointResult
                pointRes = ed.GetPoint(pointOpt);
            switch (pointRes.Status)
            {
                case PromptStatus.Cancel:
                    ed.WriteMessage("\nUser cancelled.");
                    return;
                    break;
                case PromptStatus.Error:
                    ed.WriteMessage("\nUser input error.");
                    return;
                    break;
                case PromptStatus.OK:
                    break;
            }
            ed.WriteMessage("\nYou selected point " + pointRes.Value.ToString());

        }
        //----------------------------
        public void
        GetDistance()
        {
            Editor
                 ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            PromptDistanceOptions
                distanceOpt = new PromptDistanceOptions("\nFind distance, Select first point");
            PromptDoubleResult
                distanceRes = ed.GetDistance(distanceOpt);
            switch (distanceRes.Status)
            {
                case PromptStatus.Cancel:
                    ed.WriteMessage("\nUser cancelled.");
                    return;
                    break;
                case PromptStatus.Error:
                    ed.WriteMessage("\nUser input error.");
                    return;
                    break;
                case PromptStatus.OK:
                    break;
            }
            ed.WriteMessage("\nThe distance is: " + distanceRes.Value.ToString());
        }
        //----------------------------
        public void
        GetEntity()
        {
            Editor
                ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            Database
                db = AcadApp.DocumentManager.MdiActiveDocument.Database;
            try
            {
                PromptEntityOptions
                    entOpt = new PromptEntityOptions("Select Entity");
                PromptEntityResult
                    entRes = ed.GetEntity(entOpt);
                switch (entRes.Status)
                {
                    case PromptStatus.Cancel:
                        ed.WriteMessage("\nUser cancelled.");
                        return;
                        break;
                    case PromptStatus.Error:
                        ed.WriteMessage("\nUser input error.");
                        return;
                        break;
                    case PromptStatus.OK:
                        break;
                }

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    Entity ent = (Entity)tr.GetObject(entRes.ObjectId, OpenMode.ForWrite);
                    Debug.Assert(ent != null);

                    string layerName = ent.Layer;
                    string entType;
                    RXClass rxClass = ent.GetRXClass();
                    if (rxClass == null)
                    {
                        Debug.Assert(false);
                        MessageBox.Show("AcRxObject class has not called rxInit()!");
                        entType = "*Unknown*";
                    }
                    else
                    {
                        entType = rxClass.Name;
                    }
                    ed.WriteMessage("\nEntity Type was : " + entType +
                                    " on Layer " + layerName);

                    tr.Commit();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception acEx)
            {
                ed.WriteMessage("\nError: " + acEx.Message);
                return;
            }
        }
        //----------------------------
    }
    #endregion




    #region Public class StartApp
    //-------------------------------------------------------------------------------
    /// <summary>
    /// The entry point for AutoCAD.
    /// </summary>
    public class
    kwbLab02StartApp : IExtensionApplication
    {
        public void
        Initialize()
        {
            Editor
                ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

            Commands.Lab02Help();
            ed.WriteMessage("\n  Type 'Lab02Help' at the Command Line for assistance ...");

        }
        public void
        Terminate()
        {
            // Does Nothing ... can't unload NET assembly ...
        }
    }
    #endregion
}
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.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Lab 02 Reworked ..
« Reply #1 on: December 03, 2005, 12:22:19 AM »
Any chance of a doc that helps us follow along?  Im not suggesting posting Adesks, but in your own words.  Reason I ask, is when you get to Lab03, Im stuck with why it wont work, and maybe if the doc were written better, I could figure out what Im doing wrong.
« Last Edit: December 03, 2005, 01:35:43 AM by CmdrDuh »
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Lab 02 Reworked ..
« Reply #2 on: December 03, 2005, 05:40:52 AM »
Hi CmdrDuh,

Yep, Lab 3 has a lot behind it .. The first lab dealing with the DataBase.
I know they forgot about those of us working in metric .. wasn't important, just couldn't get the blocks to look the same untill I realised that the MTEXT size should be changed.

I've popped you an email which may answer the question you have.

I'm a bit pushed for time to write a doc, but will sure try to address any questions you have , though keep in mind that I'm learning too, and some answers may not be as definitive as we'd like :-)




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.

Draftek

  • Guest
Re: Lab 02 Reworked ..
« Reply #3 on: December 03, 2005, 06:30:04 AM »
wow. Nice Kerry,

I would make one suggestion to promote scalability.

Seperate the helper methods in a seperate project / projects. The student would have completed something to directly use in thier own projects when finished with the labs.

Of course you would have to return items to the calling methods. For example, you could add entity type parameter to the GetEntity and return the entity and / or a bool to let the calling method know what happened.

again, Thanks.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Lab 02 Reworked ..
« Reply #4 on: December 03, 2005, 07:38:02 AM »
Hi Draftek,
I agree, seperation /modularization is the way to go generally.

I've found working with the Labs to be sortof evolutionary,  "ok, that works, now what if .." sort'a. Before you know it there's 5 different versions of the same method, and half a dozen obscure test routines to take care of. I think once I learn to come to grips with managing that I'll be fine.

I'm looking forward to getting the full version of VS2005 ... not being able to debug with the 2005Express versions < though I do change back to VS2003> is a real bummer, particularly for an old lisper who is used to a bit more transparency.   
« Last Edit: December 03, 2005, 07:48: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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Lab 02 Reworked ..
« Reply #5 on: December 16, 2005, 10:31:25 PM »
.....
I'm looking forward to getting the full version of VS2005 ... not being able to debug with the 2005Express versions < > is a real bummer ...............

*w00t*  Off to buy a DVD reader .. the VS2005 pack is on the way .. "I wish me a merry christmas" ...
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.