Author Topic: Create dimension from modaless dialoge  (Read 1757 times)

0 Members and 1 Guest are viewing this topic.

svb4

  • Guest
Create dimension from modaless dialoge
« on: September 30, 2011, 03:29:09 AM »
Hello,

i have written a little dialog with one button. And this button call a function whicht should insert a aligned dimension. But when I click on the button the dimension doesn't appear.
But when I run the function with a command in the commandline, then it works!

What I have to do that it also works in the dialog?

Here ist the code:
Quote
private void button1_Click(object sender, EventArgs e)
        {
            CreateAlignedDim();
        }

        public void CreateAlignedDim()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    using (doc.LockDocument())
                    {
                        using (BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable)
                        {
                            BlockTableRecord btr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                            Point3d pHilfslinienPunkt1 = new Point3d(1000.0, 0.0, 0.0);
                            Point3d pHilfslinienPunkt2 = new Point3d(3000.0, 0.0, 0.0);

                            Point3d pBemaßungslinienpunkt = new Point3d(2000.0, 500.0, 0.0);

                            AlignedDimension rd = new AlignedDimension(
                                pHilfslinienPunkt1,
                                pHilfslinienPunkt2,
                                pBemaßungslinienpunkt,
                                "",
                                db.Dimstyle) as AlignedDimension;

                            ObjectId objId = btr.AppendEntity(rd);
                            trans.AddNewlyCreatedDBObject(rd, true);
                        }
                    }
                    trans.Commit();
                }
                catch (System.Exception ex)
                {
                }
            }
        }
thanks and regards,
Patrick

svb4

  • Guest
Re: Create dimension from modaless dialoge
« Reply #1 on: September 30, 2011, 05:40:19 AM »
I've solved this topic.

If anyone need the solution, here it is:

The problem is, that i placed the document lock after i start the transaction. Now i placed it before i start the transaction and now it works.

It is very simple but it takes me one day ...  :ugly:

regards,
Patrick

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Create dimension from modaless dialoge
« Reply #2 on: September 30, 2011, 08:59:08 AM »
Is LockDocument necessary since your using a transaction?
Revit 2019, AMEP 2019 64bit Win 10

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Create dimension from modaless dialoge
« Reply #3 on: October 01, 2011, 05:11:56 AM »

Welcome to theSwamp svb4 :)
thanks for posting back.


Is LockDocument necessary since your using a transaction?

yes.
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.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Create dimension from modaless dialoge
« Reply #4 on: October 01, 2011, 09:45:40 AM »
Hi svb4,

You do not need to nest the using (Transaction ...) {...} statement within the using (DocumentLock ...) {...} one. You can write more than one using expression for the same code block.

You do not need to explicitly dispose the BlockTable object (using (BlockTable ...) { ... }). As it is opened with the transaction, it will be automatically disposed with the transaction.

Code: [Select]
try
{
    using (doc.LockDocument())
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable
        ....
    }
}
catch {}

See a more complete example here.
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Create dimension from modaless dialoge
« Reply #5 on: October 02, 2011, 12:18:10 AM »
Is LockDocument necessary since your using a transaction?

Further to my original simple response to this query ;

The primary keyword for further searches is 'Application Context'
Code that runs from the Menu, Toolbars, Palettes, and Modeless Forms runs in what is termed application context where the application is NOT aware of which document the code is meant to interact with, unlike commands originating from the command line which will be operating in 'Document Context'.
To make AutoCAD aware of the document the code intends to interact with the calling code should 'lock' the document so it can be targeted by the code instruction.

this is done by using the LockDocument method of the targeted document

IE
using (doc.LockDocument())
{
    // do the mojo here
    // <..>
} // the using mechanism unlocks the document.

Regards
Kerry
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.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Create dimension from modaless dialoge
« Reply #6 on: October 03, 2011, 07:45:16 AM »
TY Kerry for that explanation.
Revit 2019, AMEP 2019 64bit Win 10