TheSwamp

Code Red => .NET => Topic started by: David Hall on December 26, 2007, 11:11:16 AM

Title: RootNamespace
Post by: David Hall on December 26, 2007, 11:11:16 AM
Quote
Warning   1   Load of property 'RootNamespace' failed.  The string for the root namespace must be a valid identifier.   VbMgdAcad3

What does this mean?  I can not find the property referenced, and the .Net dll loads and runs fine. :-(
Title: Re: RootNamespace
Post by: Kerry on December 26, 2007, 11:41:43 AM

Hi David,

What Add-In's are you using ? and which IDE ?

Does it display when the IDE opens or at build time ..
.. The message rings a bell, but I can't recall the details.

VbMgdAcad3 ?? is that the Project ?


Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 11:47:08 AM
Im not sure what addins Im using (guess I should know that)  Im using the Acad create a project wizard thingy, but I cant remember where I got it.  Maybe the ARX SDK?

The IDE is VB2005 Express, Im working on some examples from the book that Im trying to convert to C#

It shows up at build time, as a warning, not an error, which is why I think the code still runs.

VbMgdAcad3 is the project name, default name from the wizard.
Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 11:47:57 AM
As a test, I created a new project, didn't add a thing, did a build and still generated the error, so I think its coming from the wizard thing
Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 11:48:39 AM
Per MSDN, if you go to project properties, you can clear the RootNamespace entry, but that didn't fix it either.

Quote
If you wish to specify an existing namespace in a Namespace statement, such as in the generic list class example, you can set your root namespace to a null value. You do this by choosing Project Properties from the Project menu and then clearing the Root namespace entry so that the box is empty. If you did not do this in the generic list class example, the Visual Basic compiler would take System.Collections.Generic as a new namespace within project Payroll, with the full name of Payroll.System.Collections.Generic.
Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 11:53:05 AM
Also, on a sidenote, why do we set CopyLocal to FALSE for the 2 references acdbmgd and acmgd?  The wizard thing is setting both of those to TRUE.  I remember a year ago or so, you and Glenn helped me get some code to work by setting those to FALSE.
Title: Re: RootNamespace
Post by: Kerry on December 26, 2007, 12:02:20 PM

That's needed for debugging David, Autocad looks for them in the same folder as ACAD.exe ; having them in the same folder as your DLL causes conflicts .. I'm not sure of the exact mechanics.

ALSO, check that the LoaderLock break is toggled OFF (located Debug -> Exceptions -> Managed Debugging Assistants )
Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 12:11:56 PM
Well LoaderLock was turned on, so I set it to OFF.  What exactly does that do?  I remember a thread about it being a problem. 
Title: Re: RootNamespace
Post by: Kerry on December 26, 2007, 12:22:25 PM

Dont know the exact bitwise reason David , except as detailed in the error message :
"Attempting managed execution inside OS Loader lock. Do not attempt to run managed code
inside a DllMain or image initialization function since doing so can cause the application to hang."


ALSO
Check that 'Edit and Continue' is ON
Tools -> Options -> Debugging -> Edit and Continue
Title: Re: RootNamespace
Post by: Kerry on December 26, 2007, 12:25:16 PM
P.S.
I'm surprised you're not using the VS2008 Express.
It has the ability to target a specific NET version if needed.
Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 12:27:24 PM
I dont have that option available
Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 12:28:30 PM
I have it loaded for C# but I didn't download it for VB as Im not "planning" on using VB for my code.  Do you think 08 would make a difference for testing the way I am?
Title: Re: RootNamespace
Post by: Kerry on December 26, 2007, 12:31:11 PM
[just a suspicion]
David, Are you using the Startup Template that came with the 'book' ?

Title: Re: RootNamespace
Post by: Kerry on December 26, 2007, 12:35:56 PM
I have it loaded for C# but I didn't download it for VB as Im not "planning" on using VB for my code.  Do you think 08 would make a difference for testing the way I am?

Probably not, since you're just using it as a glorified texteditor for the vb samples. I recall someone saying all the code samples were snippets you add to the solution, is that correct?
Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 12:41:34 PM
[just a suspicion]
David, Are you using the Startup Template that came with the 'book' ?


quite possibly Im not sure.  Now that I think about it, I think I am
Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 12:43:17 PM
Probably not, since you're just using it as a glorified texteditor for the vb samples. I recall someone saying all the code samples were snippets you add to the solution, is that correct?
Yes.  I opened one of the snippets to see if there were Namespace conflicts there, but couldn't find any
Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 12:49:18 PM
I DL'd VB08, created a project manually without the wizard (Wasn't an option when I created new project) and did not generate the error msg and code ran fine.  Must be something inside the template files
Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 12:50:36 PM
And, after learning how to make a snippet today, other than adding the references to the 2 ac*.dll files, why would I want to use that template?
Title: Re: RootNamespace
Post by: Kerry on December 26, 2007, 12:50:51 PM

Probably not, since you're just using it as a glorified texteditor for the vb samples. I recall someone saying all the code samples were snippets you add to the solution, is that correct?
Yes.  I opened one of the snippets to see if there were Namespace conflicts there, but couldn't find any

I'm guessing here ..
I'd guess it's a Template or Add-in.

Do you get the same issue if you build a project environment from scratch ? If not, you could save your own template from a 'good' project.
 File -> Export Template ...

ADDED:
I note that you've posted concurrently that you've done this. (I won't delete this post just in case the post police come along :D)
Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 12:55:16 PM
No, it doesn't generate the error.  I dont think I'll use that template anymore
Quote from: Kerry Brown
If not, you could save your own template from a 'good' project.
That was what I was thinking as well.  Thanks Kerry.
Title: Re: RootNamespace
Post by: Fatty on December 26, 2007, 12:57:12 PM

Hi David,

What Add-In's are you using ? and which IDE ?

Does it display when the IDE opens or at build time ..
.. The message rings a bell, but I can't recall the details.

VbMgdAcad3 ?? is that the Project ?


VbMgdAcad is default project name of AutoCAD VB.NET Template
that installed from CD from this nice book:
"VB.NET Programming for AutoCAD Customization
VB.NET Training for AutoCAD 2007 and AutoCAD 2008"
by Jerry Winters
You can find it on www.vbcad.com
I have just one that I got as gift from my friend
Brian from California, God bless him for that

~'J'~



Title: Re: RootNamespace
Post by: David Hall on December 26, 2007, 01:02:11 PM
Yes, that is the 'book' being referenced.  Its a good book, and the examples are pretty well written.  I'm finding it pretty easy to move my VBA code to .Net using it as a guide.  On a side note, if you ever get the chance to attend one of his classes, do it.  Great speaker, and has a lot of good presentation skills that makes learning it easier.
Title: Re: RootNamespace
Post by: Kerry on December 26, 2007, 01:29:19 PM
Translating concepts to C# is a great idea.

I'd imagine that casting types for C# is one of the biggest differences.

 using (Transaction tr = tm.StartTransaction())
 {             
   BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId,OpenMode.ForRead,false);
   BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite,false);


as opposed to something like this in vb

       Dim tr As Transaction = tm.StartTransaction()
       Try
           Dim bt As BlockTable = tm.GetObject(db.BlockTableId, OpenMode.ForRead, False)
           Dim btr As BlockTableRecord = tm.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, False)


the vb compiler is more 'forgiving' about type safety ... but we won't go there ...
Title: Re: RootNamespace
Post by: Fatty on December 26, 2007, 01:36:47 PM
Agreed with you, but I should't get a chance at all

~'J'~
Title: Re: RootNamespace
Post by: Glenn R on December 26, 2007, 02:21:59 PM
First off, I sometimes get the error about 'RootNamespace' but I just ignore it, although I haven't investigated it (I suspect it's a timing issue with loading VS).

Secondly, WRT the copylocal thing I understand it as such:

When you compile a .exe, the framework will spin up an 'AppDomain' which is the framework's equivalent of a separate address space for the application to use (think WIN32 application isloation so a bad proggy won't bring windows to a GPF faster than you can say GPF) :)

Now, Acad spins up it's own AppDomain, which you load your .dll's into and all is good. Now with an AppDomain, there are rules for how it looks for supporting files (read dll's) and this is called 'probing' or assembly resolution ie how the main exe resolves and finds it's supporting files. From memory, this is the same folder as the exe or any sub-folders, but this probing can be modified by the yourExeName.exe.config file or in this case, the acad.exe.config file - I must stress that I would never, ever modify the base acad install and this DEFINATELY falls into this category.

So, you *could* modify the acad.exe.config and add the appropriate entries into this xml file to cause it to 'look' in different folders for additional files, but DON'T.

Hope this helps.
Merry Xmas from the UK.

Cheers,
Glenn.
Title: Re: RootNamespace
Post by: Fatty on December 26, 2007, 02:45:09 PM
Late is better than never :)
Happy Chrismas from Russia


~'J'~
Title: Re: RootNamespace
Post by: Glenn R on December 26, 2007, 02:57:01 PM
Merry Christmas to you too, Fatty.
Title: Re: RootNamespace
Post by: Kerry on December 28, 2007, 10:02:46 AM
David,

I had a look at one of the Methods from that book today.
It seemed a little verbose, so I had a look at an option in C# the way I thought I'd write it if I was trying to teach someone.

Here's the comparison.

The book VB code :
    <CommandMethod("DrawALineA")> _
       Sub DrawALineA()
        Dim myTransMan As DatabaseServices.TransactionManager
        Dim myTrans As DatabaseServices.Transaction
        Dim myDWG As ApplicationServices.Document
        Dim myBT As DatabaseServices.BlockTable
        Dim myBTR As DatabaseServices.BlockTableRecord

        'Get the active document and begin a Transaction
        myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
        myTransMan = myDWG.TransactionManager
        myTrans = myTransMan.StartTransaction

        'Open the BlockTable for Read
        myBT = myDWG.Database.BlockTableId.GetObject( _
            DatabaseServices.OpenMode.ForRead)
        myBTR = myBT(DatabaseServices.BlockTableRecord.ModelSpace).GetObject( _
            DatabaseServices.OpenMode.ForWrite)

        'Draw the line
        Dim StartPoint As New Geometry.Point3d(0, 0, 0)
        Dim EndPoint As New Geometry.Point3d(4, 5, 6)
        Dim myLine As New DatabaseServices.Line(StartPoint, EndPoint)
        myBTR.AppendEntity(myLine)
        myTrans.AddNewlyCreatedDBObject(myLine, True)

        'Commit the Transaction
        myTrans.Commit()

        myTrans.Dispose()
        myTransMan.Dispose()
    End Sub
   
 


my C# hack
       
[CommandMethod("Cmd1")]
    static public void AddLineToModelSpace()
    {
        // Calculate the Line extent
        Point3d startPoint = new Point3d(1, 2, 3);
        Point3d endPoint = new Point3d(4, 5, 6);

        // Start a Drawing DataBase Transaction
        Database db = AcadApp.DocumentManager.MdiActiveDocument.Database;
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            // Define the Line
            Line lineEnt = new Line(startPoint, endPoint);

            ((BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db),
                    OpenMode.ForWrite, false)).AppendEntity(lineEnt);

            tr.AddNewlyCreatedDBObject(lineEnt, true);
            tr.Commit();
        }
    }


Now I'm sure the VB code could be trimmed, but why wasn't it ?


Title: Re: RootNamespace
Post by: It's Alive! on December 28, 2007, 10:14:45 AM
...Now I'm sure the VB code could be trimmed, but why wasn't it ?

It’s probably because one needs to speak more slowly for VB users.
Title: Re: RootNamespace
Post by: Kerry on December 28, 2007, 10:18:21 AM
 Dim   as joke


this could get out of hand ... but Mav's on holidays so it may be ok.
Title: Re: RootNamespace
Post by: sinc on December 28, 2007, 10:20:11 AM
joke.AddRimShot()
Title: Re: RootNamespace
Post by: It's Alive! on December 28, 2007, 10:27:23 AM
I find it strange the points and line are created inside the transaction. ….but maybe I’m just that way..

One more thing, I haven’t seen the book, so I don’t know the context of how this sample was presented,
but one thing I would certainly try to hammer home from the start is the need for handling exceptions.
Title: Re: RootNamespace
Post by: sinc on December 28, 2007, 12:43:05 PM
Speaking of exception handling, it would also be nice if the Autodesk developer documentation included the possible exceptions that various methods can throw...  The core .NET documentation from Microsoft typically includes this information, but the Autodesk stuff leaves something to be desired.
Title: Re: RootNamespace
Post by: Glenn R on December 28, 2007, 03:15:30 PM
Kerry,

Before I go on a rant, can you confirm something for me please? Is the VB code you posted, a direct copy, of what's written in Mr. Winters book?

Cheers,
Glenn.
Title: Re: RootNamespace
Post by: Kerry on December 28, 2007, 05:27:13 PM

yes Glenn, cut and paste unfortunately.

There are [lots] of examples in a similar vein. By the time osmosis takes effect (firsthand and inherited) this will/may become the acccepted standard for VB code.

Title: Re: RootNamespace
Post by: Glenn R on December 28, 2007, 05:39:32 PM
Kerry,

Excellent - thanks for the confirmation...the rant will now continue :)

Do you see, what is, dare I say it, catastrophically wrong, with the code demonstrated by Mr. Winters and why nobody should follow this as a 'good' example?

Cheers,
Glenn.
Title: Re: RootNamespace
Post by: Kerry on December 28, 2007, 07:37:42 PM
Kerry,

Excellent - thanks for the confirmation...the rant will now continue :)

Do you see, what is, dare I say it, catastrophically wrong, with the code demonstrated by Mr. Winters and why nobody should follow this as a 'good' example?

Cheers,
Glenn.

Glenn,
I must admit that VB code makes my eyes glaze over and I didn't really look at the example code from a technical viewpoint until you raised the flag.

I'm usually reticent about knocking peoples efforts but I think in this case it must be done .. particularly as the sample posted is purported as being a template for drawing anything in ModelSpace.

Title: Re: RootNamespace
Post by: Kerry on December 28, 2007, 07:55:30 PM
Speaking of exception handling, it would also be nice if the Autodesk developer documentation included the possible exceptions that various methods can throw...  The core .NET documentation from Microsoft typically includes this information, but the Autodesk stuff leaves something to be desired.

Richard,
The only way this will happen is if enough people contact Kean Walmsley and Stephen Preston at AutoDesk with a compelling argument.
Title: Re: RootNamespace
Post by: Kerry on December 28, 2007, 09:13:35 PM
A couple of things ...
aside from the lack of a using keyword  for the transaction which wraps an object implementing IDisposable for automatic disposal.  ..

The sample code does not use the transaction’s GetObject member to determine the BlockTable or BlockTableRecord,
it uses the myDWG.Database.BlockTableId.GetObject( .. and  (DatabaseServices.BlockTableRecord.ModelSpace).GetObject( ...

This is (by my understanding) contrary to the ARX documentation and does not follow the guidelines established in the AutoCAD Managed Training Labs ; particularly Lab 3 – Database Fundamentals: Creating our Employee Object ...
where all object references are obtained through the transaction, not via a direct open/close paradigm.
added:
essentially, this sample is mixing methods for obtaining the references ... and my understanding is that if you do that "bad stuff can happen"

I'm sure that this could be explained better by someone coming from an ARX background.

I'd be happy to be shown incorrect, because I am imagining all the VB code currently being copied using this sample template.

Title: Re: RootNamespace
Post by: sinc on December 29, 2007, 12:42:22 AM
No, that's not a problem.

The ObjectId.GetObject() method implicitly uses the top-level transaction.  So it is only an error to call ObjectId.GetObject() if there is no current transaction.  The VB code given starts a transaction, so that should be fine.

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.
Title: Re: RootNamespace
Post by: sinc on December 29, 2007, 12:43:28 AM
Speaking of exception handling, it would also be nice if the Autodesk developer documentation included the possible exceptions that various methods can throw...  The core .NET documentation from Microsoft typically includes this information, but the Autodesk stuff leaves something to be desired.

Richard,
The only way this will happen is if enough people contact Kean Walmsley and Stephen Preston at AutoDesk with a compelling argument.


A compelling argument shouldn't be necessary.  It should be blatantly obvious that API documentation is not complete if it does not document the exceptions.
Title: Re: RootNamespace
Post by: Kerry on December 29, 2007, 02:07:14 AM
No, that's not a problem.

The ObjectId.GetObject() method implicitly uses the top-level transaction.  So it is only an error to call ObjectId.GetObject() if there is no current transaction.  The VB code given starts a transaction, so that should be fine.


Richard, this is from Reflection in IL ..
From the VB code
    L_0048: call instance class [acdbmgd]Autodesk.AutoCAD.DatabaseServices.DBObject [acdbmgd]Autodesk.AutoCAD.DatabaseServices.ObjectId::GetObject(valuetype [acdbmgd]Autodesk.AutoCAD.DatabaseServices.OpenMode)
    L_004d: castclass [acdbmgd]Autodesk.AutoCAD.DatabaseServices.BlockTableRecord
<...>


From the C# code

    L_0076: callvirt instance class [acdbmgd]Autodesk.AutoCAD.DatabaseServices.DBObject [acdbmgd]Autodesk.AutoCAD.DatabaseServices.Transaction::GetObject(valuetype [acdbmgd]Autodesk.AutoCAD.DatabaseServices.ObjectId, valuetype [acdbmgd]Autodesk.AutoCAD.DatabaseServices.OpenMode, bool)
    L_007b: castclass [acdbmgd]Autodesk.AutoCAD.DatabaseServices.BlockTableRecord
<...>

it seems to me the VB does not use the transaction.
I may be placing too much importance on the things I've read .. or misunderstanding them , but the docs ARE pretty clear ...
Title: Re: RootNamespace
Post by: sinc on December 29, 2007, 10:12:23 AM
Yeah, I think the docs are pretty clear, too.

This is from the documentation for the DatabaseServices.ObjectId.GetObject() method:

Quote
Overloaded. This method calls the TransactionManager.GetObject() method of the top transaction passing on all the arguments it has received.

Transactions may be "nested".  You can create a transaction, then in the middle of processing that transaction, you can create and commit another transaction.  Each time you start a new transaction, you add it to the top of the stack.  GetObject() automatically uses the top-most transaction on the stack (i.e., the last one you created).  It should be functionally equivalent to calling Transaction.GetObject().  I'm guessing that calling GetObject() on the Transaction directly may result in slightly-better performance, because the ObjectId.GetObject() method would need to figure out which transaction to use, but the difference is probably minimal.  We'd have to do a benchmark test to actually find out if there's any significant time difference between the two.
Title: Re: RootNamespace
Post by: David Hall on December 31, 2007, 10:52:01 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 ?
Title: Re: RootNamespace
Post by: David Hall on December 31, 2007, 11:53:30 AM
Kerry, you have shown some very interesting things in the compressed code example to draw a line.  the 2 things that jump out at me right away are the casting of BTR to the TR instead of creating a BTR, and the use of SymbolUtilityServices.GetBlockModelspaceId.  The first I kinda understand, your just shortcutting the process.  The second, I didn't even know existed.  In the SymbolUtilityServices, what is the bool openedErased at the end used for?
Title: Re: RootNamespace
Post by: It's Alive! 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
Title: Re: RootNamespace
Post by: David Hall 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
Title: Re: RootNamespace
Post by: David Hall 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
Title: Re: RootNamespace
Post by: David Hall 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?
Title: Re: RootNamespace
Post by: David Hall 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 ==
Title: Re: RootNamespace
Post by: It's Alive! 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
Title: Re: RootNamespace
Post by: sinc 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?
Title: Re: RootNamespace
Post by: It's Alive! 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.
Title: Re: RootNamespace
Post by: David Hall 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
Title: Re: RootNamespace
Post by: David Hall 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);
                }

            }
Title: Re: RootNamespace
Post by: It's Alive! 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
Title: Re: RootNamespace
Post by: David Hall on December 31, 2007, 02:15:39 PM
Good way to look at it.  Thanks
Title: Re: RootNamespace
Post by: Bryco 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.