.NET

Reply
Employee
2 Posts
0 Kudos
Registered: ‎10-25-2012
Post 1 of 4
Accepted Solution

Trouble with Transactions

367 Views, 3 Replies
10-30-2012 09:20 PM

Hi all,

 

Greatly appreciate any help. I'm creating a winforms app with a button that lets the user create a boundary from some closed areas in the drawing (via TraceBoundary), and calculates the area of the boundary.

 

Before calculating the area, I want to add the boundary object (a pline) to the model space via the Using Transaction...AppendEntity...AddNewlyCreatedDBObject strategy (as seen here), and have a prompt pop up to ask the user if the correct boundary was created. My problem below is that the boundary doesn't appear before the prompt. It only shows up after the entire transaction commits. Is there a way to have it appear before the prompt?

 

Please see some screenshots of my problem attached.

 

private void Button_Click(object sender, EventArgs e)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            db.LineWeightDisplay = true;

            double boundedarea = 0;
            bool keeplooping = true;
            ObjectIdCollection ids = new ObjectIdCollection();

            using (EditorUserInteraction edUserInt = ed.StartUserInteraction(this))
            {
                while (keeplooping)
                {
                    PromptPointOptions ptOptions = new PromptPointOptions("\nSelect point within area ");
                    ptOptions.AllowNone = false;
                    PromptPointResult ptResult = ed.GetPoint(ptOptions);
                    if (ptResult.Status != PromptStatus.OK)
                        return;

                    DBObjectCollection collection = ed.TraceBoundary(ptResult.Value, true);

                    using (Transaction Tx = db.TransactionManager.StartTransaction())
                    {
                        ObjectId ModelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(db);
                        BlockTableRecord model = Tx.GetObject(ModelSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                        Polyline pline = collection[0] as Polyline;
                        if (pline != null)
                        {
                            pline.ColorIndex = 1;
                            pline.LineWeight = LineWeight.LineWeight050;
                            ids.Add(model.AppendEntity(pline));
                            Tx.AddNewlyCreatedDBObject(pline, true);
                            boundedarea = pline.Area;
                            ed.Regen(); //I'd like the boundary from TraceBoundary to be red and thick at this point
                            //so when kwOptions is run, the user can see if they selected the correct area

                            PromptKeywordOptions kwOptions = new PromptKeywordOptions("\nIs this the correct area? ");
                            kwOptions.Keywords.Add("Yes");
                            kwOptions.Keywords.Add("No");
                            kwOptions.Keywords.Default = "Yes";
                            kwOptions.AllowNone = true;
                            PromptResult pKeyRest = ed.GetKeywords(kwOptions);

                            if (pKeyRest.StringResult == "Yes")
                            {
                                textBox1.Text = boundedarea.ToString();
                                keeplooping = false;
                                Tx.Commit();
                            }
                            else
                                Tx.Abort();
                        }
                    }
                }
                edUserInt.End();
            }

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
(this .cs contains a lot of other methods - not all assemblies relevant here)

 

Use the TransactionManager from the Document rather than the one from the Database, and just before you start each transaction, call the TransactionManager's EnableGraphicsFlush() method. 

 

You will also need to refactor your code so that Commit() is called on each Transaction at the point where you want the newly added object to become visible. Also call Editor.UpdateScreen() after each call to Commit(), and with that you should not need to call Editor.Regen().

 

You can prompt for input after the transaction is committed but it is the call to Commit() that should trigger the graphics update.

Valued Mentor
370 Posts
175 Kudos
Registered: ‎05-06-2012
Post 2 of 4

Re: Trouble with Transactions

10-31-2012 04:50 AM in reply to: brian.young

Use the TransactionManager from the Document rather than the one from the Database, and just before you start each transaction, call the TransactionManager's EnableGraphicsFlush() method. 

 

You will also need to refactor your code so that Commit() is called on each Transaction at the point where you want the newly added object to become visible. Also call Editor.UpdateScreen() after each call to Commit(), and with that you should not need to call Editor.Regen().

 

You can prompt for input after the transaction is committed but it is the call to Commit() that should trigger the graphics update.

Employee
2 Posts
0 Kudos
Registered: ‎10-25-2012
Post 3 of 4

Re: Trouble with Transactions

10-31-2012 09:01 AM in reply to: DiningPhilosopher

Ah, thank you!

 

turns out the graphics flush did the trick! More info here: http://www.theswamp.org/index.php?topic=30185.0

 

I replaced the ed.Regen() with Tx.TransactionManager.QueueForGraphicsFlush() and it works perfectly.

 

Thanks

Brian

Mentor
182 Posts
11 Kudos
Registered: ‎01-06-2003
Post 4 of 4

Re: Trouble with Transactions

12-15-2012 11:46 PM in reply to: brian.young

Could someone point me to some information regarding the difference and different uses of TransactionManager from the Document rather than the one from the Database? Thanks, Dale

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post