Author Topic: RootNamespace  (Read 23140 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: RootNamespace
« Reply #45 on: December 31, 2007, 11:59:12 AM »
Except for the failure to use the "using" keyword.  That isn't exactly an error - the "using" keyword is really just a syntactical shortcut - but if the code doesn't use "using", then it should use a try-finally block, and put the Dispose() calls in the Finally block.
OK, so if I understand what your saying, IF you use a "using" statement there is no need for a Try/Catch ?


The “using” statement it for IDisposable Objects. It does not replace try catch
See http://msdn2.microsoft.com/en-us/library/yh598w02.aspx

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: RootNamespace
« Reply #46 on: December 31, 2007, 12:48:32 PM »
Code: [Select]
        [CommandMethod("DL2")]
        public void DrawLine2()
        {
            PromptPointOptions PPO = new Autodesk.AutoCAD.EditorInput.PromptPointOptions("\nSelect Point");
            PromptPointResult PPR ;
            Point3d sp;
            Point3d ep = new Point3d(4, 5, 0);
            Database DB = Application.DocumentManager.MdiActiveDocument.Database;
            using (Transaction tr = DB.TransactionManager.StartTransaction())
            {
                try
                {
                    PPR= Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(PPO);
                    if (PPR.Status = Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
                    {
                        Line lineEnt = new Line(sp, ep);
                        ((BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(DB), OpenMode.ForWrite, false)).AppendEntity(lineEnt);
                        tr.AddNewlyCreatedDBObject(lineEnt, true);
                    }
                   
                }
                finally
                {
                    tr.Commit();
                }
            }
        }
I cant seem to make this work.  It is crashing on the if PPR.Status line
« Last Edit: December 31, 2007, 02:00:44 PM 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)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: RootNamespace
« Reply #47 on: December 31, 2007, 12:49:18 PM »
Im typing out fully qualified names to help myself learn where things are, thus the LOOOONG lines above
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)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: RootNamespace
« Reply #48 on: December 31, 2007, 12:52:08 PM »
it says
Quote
Cannot implicitly convert type 'Autodesk.AutoCAD.EditorInput.PromptStatus' to 'bool'
which I understand, but what I dont get is why is it trying to?  Im just asking if PPR.Status=Autodesk.AutoCAD.EditorInput.PromptStatus.OK  Shouldn't that eval as True or False?
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)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: RootNamespace
« Reply #49 on: December 31, 2007, 12:55:27 PM »
Nevermind, I just pulled a Duhism!  I needed
Quote
if (PPR.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
Note the ==
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)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: RootNamespace
« Reply #50 on: December 31, 2007, 12:59:51 PM »
Nevermind, I just pulled a Duhism!  I needed
Quote
if (PPR.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
Note the ==


 :laugh:

Code: [Select]
   [CommandMethod("DL2")]
    public void DrawLine2()
    {
      PromptPointOptions PPO = new Autodesk.AutoCAD.EditorInput.PromptPointOptions("\nSelect Point");// \n not /n
      PromptPointResult PPR = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(PPO);
      if (PPR.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK) //== not =
      {
        Point3d sp = PPR.Value;
        Point3d ep = new Point3d(4, 5, 0);
        Database DB = Application.DocumentManager.MdiActiveDocument.Database;
        Line lineEnt = new Line(sp, ep);
        using (Transaction tr = DB.TransactionManager.StartTransaction())
        {
          try
          {
            ((BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(DB), OpenMode.ForWrite, false)).AppendEntity(lineEnt);
            tr.AddNewlyCreatedDBObject(lineEnt, true);
            tr.Commit();
          }
          catch (System.Exception ex)
          {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage(ex.Message);
          }
        }
      }
    }

edited code again
« Last Edit: December 31, 2007, 01:35:10 PM by Daniel »

sinc

  • Guest
Re: RootNamespace
« Reply #51 on: December 31, 2007, 01:07:18 PM »
I'm used to always either committing or aborting a transaction.

I notice in your code, Daniel, you commit the transaction if it succeeds.  But if an exception is thrown, the transaction is neither closed nor aborted.

Is this OK?  Does the implicit Dispose() call (from the "using" statement) automatically abort the transaction?

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: RootNamespace
« Reply #52 on: December 31, 2007, 01:43:03 PM »
I'm used to always either committing or aborting a transaction.

I notice in your code, Daniel, you commit the transaction if it succeeds.  But if an exception is thrown, the transaction is neither closed nor aborted.

Is this OK?  Does the implicit Dispose() call (from the "using" statement) automatically abort the transaction?

I think so, could be wrong about this. But if this transaction it not committed before the transaction object goes out of scope it will be aborted by default.
So if an exception is thrown the transaction will abort, which is probably the best course of action.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: RootNamespace
« Reply #53 on: December 31, 2007, 02:00:19 PM »
Daniel, I noticed you rearranged my code a little, which is cool.  Question is why? Wouldn't you want
Quote
PromptPointOptions PPO = new Autodesk.AutoCAD.EditorInput.PromptPointOptions("\nSelect Point");// \n not /n
      PromptPointResult PPR = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(PPO);
inside the try?  My thinking that if the user hits ESC inside the try, it aborts

BTW, I caught the \n but I guess you posted before I updated my code
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)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: RootNamespace
« Reply #54 on: December 31, 2007, 02:09:13 PM »
updated code
Code: [Select]
            using (Transaction tr = DB.TransactionManager.StartTransaction())
            {
                try
                {
                    PPR= Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(PPO);
                    if (PPR.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
                    {
                        Line lineEnt = new Line(PPR.Value , ep);
                        ((BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(DB), OpenMode.ForWrite, false)).AppendEntity(lineEnt);
                        tr.AddNewlyCreatedDBObject(lineEnt, true);
                        tr.Commit();
                    }
                    else
                    {
                        tr.Abort();
                    }
                   
                }
                catch (System.Exception ex)
                {
                    Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
                    ed.WriteMessage(ex.Message);
                }

            }
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)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: RootNamespace
« Reply #55 on: December 31, 2007, 02:12:16 PM »
Daniel, I noticed you rearranged my code a little, which is cool.  Question is why? Wouldn't you want
Quote
PromptPointOptions PPO = new Autodesk.AutoCAD.EditorInput.PromptPointOptions("\nSelect Point");// \n not /n
      PromptPointResult PPR = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(PPO);
inside the try?  My thinking that if the user hits ESC inside the try, it aborts

BTW, I caught the \n but I guess you posted before I updated my code

The way I have the code, if the user hits escape the transaction is never opened  I.e.
Code: [Select]
if (PPR.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK)Then run the rest of the code. You could put an else statement and write a nice little message to the user

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: RootNamespace
« Reply #56 on: December 31, 2007, 02:15:39 PM »
Good way to look at it.  Thanks
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)

Bryco

  • Water Moccasin
  • Posts: 1882
Re: RootNamespace
« Reply #57 on: December 31, 2007, 04:08:32 PM »
I've been using if (PPR.Status != PromptStatus.OK) return;
before the try, it saves those long if statements and indents.