Routine Running Twice?

Routine Running Twice?

lmorse
Contributor Contributor
612 Views
3 Replies
Message 1 of 4

Routine Running Twice?

lmorse
Contributor
Contributor

Hi,

 

I have written the following code, it reads co-ordinates from an Access Database and plots the points in a drawing. My issue is that the command appears to run twice (I get the console messages twice and duplicate points). It happens if I run debug and if I run the app directly without debug. Can anyone see an issue with my code? Or is the issue somewhere else?

 

using System.Data.OleDb;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;



namespace ReadDatabase
{
    public class DBReader
    {
        [CommandMethod("ReadSurveyDatabase")]
        public void ReadSurveyDatabase()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            
            ed.WriteMessage("\n-------------------UTILITY TO READ FROM SURVEY DATABASE---------------------------");
            // Connection string and SQL query    
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"W:\\Technical Services\\DataBase\\Grosvenor Survey_be.accdb\"";
            string strSQL = "SELECT * FROM tMASTER_BOREHOLE_LIST";
            
            ed.WriteMessage("\n    -Connecting to database.");
            // Create a connection    
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                // Create a command and set its connection    
                OleDbCommand command = new OleDbCommand(strSQL, connection);
                // Open the connection and execute the select command.

                bool success;
                
                ed.WriteMessage("\n    -Checking and creating layers.");
                success = LayerCheck();

                if(success != true)
                {
                    Application.ShowAlertDialog("Layer Check Failed.");                    
                }

                try
                {
                    // Open connecton    
                    connection.Open();
                    // Execute command    
                    using (OleDbDataReader reader = command.ExecuteReader())
                    {
                        
                        ed.WriteMessage("\n    -Reading database.");
                        using (Transaction transact = db.TransactionManager.StartTransaction())
                        {
                            BlockTable bt;
                            bt = transact.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                            BlockTableRecord btr;
                            btr = transact.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                            double easting = 0.0;
                            double northing = 0.0;
                            double RL = 0.0;
                            
                            while (reader.Read())
                            {
                                if (reader["AS_DRILLED_SURVEY_E"].ToString() != "")
                                {
                                    easting = double.Parse(reader["AS_DRILLED_SURVEY_E"].ToString());
                                }
                                else if(reader["AGD84_AMG_X"].ToString() != "")
                                {
                                    easting = double.Parse(reader["AGD84_AMG_X"].ToString());
                                }
                                else
                                {
                                    easting = 0.0;
                                }

                                if (reader["AS_DRILLED_SURVEY_N"].ToString() != "")
                                {
                                    northing = double.Parse(reader["AS_DRILLED_SURVEY_N"].ToString());
                                }
                                else if (reader["AGD84_AMG_Y"].ToString() != "")
                                {
                                    northing = double.Parse(reader["AGD84_AMG_Y"].ToString());
                                }                                
                                else
                                {
                                    northing = 0.0;
                                }
                                if (reader["AS_DRILLED_SURVEY_RL"].ToString() != "")
                                {
                                    RL = double.Parse(reader["AS_DRILLED_SURVEY_RL"].ToString());
                                }
                                else if (reader["RL"].ToString() != "")
                                {
                                    RL = double.Parse(reader["RL"].ToString());
                                }
                                else
                                {
                                    RL = 0.0;
                                }

                                string boreholeName = reader["HoleID"].ToString();
                                //ed.WriteMessage("\n{0}:  {1}  {2}  {3}", boreholeName, easting.ToString(), northing.ToString(), RL.ToString());
                                
                                try
                                {
                                    Point3d boreholePoint = new Point3d(easting, northing, RL);
                                    Point3d mtextInsertPoint = new Point3d(easting + 0.1, northing + 0.1, RL);
                                    if ((easting > 0.0) & (northing > 0.0) & boreholeName != "")
                                        //if ((easting > 0.0) & (northing > 0.0) & (RL > 0.0) & boreholeName != "")
                                        {
                                        //ed.WriteMessage("\nDrawing Borehole Object: ");
                                        using (MText mtx = new MText())
                                        {
                                            mtx.Contents = boreholeName;
                                            mtx.Location = mtextInsertPoint;
                                            mtx.TextHeight = 3;

                                            btr.AppendEntity(mtx);
                                            transact.AddNewlyCreatedDBObject(mtx, true);
                                        }
                                        using (DBPoint point = new DBPoint(boreholePoint))
                                        {
                                            point.ColorIndex = 1;
                                            btr.AppendEntity(point);
                                            transact.AddNewlyCreatedDBObject(point, true);
                                        }
                                    }
                                    

                                }
                                catch (System.Exception ex)
                                {

                                    Application.ShowAlertDialog("Error Encountered: " + ex.Message);
                                    transact.Abort();
                                    break;
                                }
                            }
                            ed.WriteMessage("\n    -Completed.");
                            // Set the style for all point objects in the drawing
                            db.Pdmode = 34;
                            db.Pdsize = 0.3;
                            transact.Commit();
                            doc.SendStringToExecute(". zoom e ", false, false, false);
                            
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    Application.ShowAlertDialog("Error: " + ex.Message);

                }
                // The connection is automatically closed becasuse of using block.    
            }
            return;
        }

        public static bool LayerCheck()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            using (Transaction transact = db.TransactionManager.StartTransaction())
            {
                // DEFAULT LAYER DEFINITIONS
                string[] LayerNames = new string[12];
                LayerNames[0] = "XXX - GROSVENOR GAS DRAINAGE FLIGHT PATH";
                LayerNames[1] = "XXX - GROSVENOR GOAF GAS DRAINAGE HOLES - LAYER MUST BE TURNED ON";
                LayerNames[2] = "XXX - GROSVENOR GOAF GAS DRAINAGE HOLES GROUTED - LAYER MUST BE TURNED ON";
                LayerNames[3] = "XXX - GROSVENOR GOAF NITROGEN HOLES - LAYER MUST BE TURNED ON";
                LayerNames[4] = "XXX - GROSVENOR HOLES - LAYER MUST BE TURNED ON";
                LayerNames[5] = "XXX - GROSVENOR MONITORING HOLES - LAYER MUST BE TURNED ON";
                LayerNames[6] = "XXX - GROSVENOR MONITORING HOLES GROUTED - LAYER MUST BE TURNED ON";
                LayerNames[7] = "XXX - LEGACY BOREHOLES - LAYER MUST BE TURNED ON";
                LayerNames[8] = "XXX - LEGACY BOREHOLES GROUTED - LAYER MUST BE TURNED ON";
                LayerNames[9] = "XXX - MINEX SIGNIFICANTLY DIFFERENT MAY 2017 - LAYER MUST BE TURNED ON";
                LayerNames[10] = "XXX - OPEN SERVICE HOLES - LAYER MUST BE TURNED ON";
                LayerNames[11] = "XXX - OPEN SERVICE HOLES FLIGHT PATH";
                //LayerNames[13] = "";

                short[] layerColour = new short[12];
                layerColour[0] = 1;
                layerColour[1] = 5;
                layerColour[2] = 253;
                layerColour[3] = 1;
                layerColour[4] = 5;
                layerColour[5] = 253;
                layerColour[6] = 5;
                layerColour[7] = 8;
                layerColour[8] = 4;
                layerColour[9] = 253;
                layerColour[10] = 1;
                layerColour[11] = 6;
                //layerColour[0] = 1;

                try
                {
                    // Check through the layers and make sure they exist
                    LayerTable layTable = transact.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                    for (int i = 0; i <= LayerNames.Length -1; i++)
                    {
                        if (LayerNames[i].ToString() != "" & LayerNames[i].ToString() != null)
                        {
                            if (layTable.Has(LayerNames[i].ToString()) == false)
                            {
                                LayerTableRecord layTableRecord = new LayerTableRecord();
                                layTableRecord.Name = LayerNames[i].ToString();
                                layTableRecord.Color = Color.FromColorIndex(ColorMethod.ByAci, layerColour[i]);
                                layTable.UpgradeOpen();
                                layTable.Add(layTableRecord);
                                transact.AddNewlyCreatedDBObject(layTableRecord, true);
                            }
                        }                        
                    }
                    transact.Commit();
                    return true;
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage("Error: " + ex.Message);
                    transact.Abort();
                    return false;
                }
                   
            }
        }
    }
}
0 Likes
Accepted solutions (1)
613 Views
3 Replies
Replies (3)
Message 2 of 4

hippe013
Advisor
Advisor
Accepted solution

It appears that the problem is with your zoom extents. The space after the 'e' is the offender. 

doc.SendStringToExecute(". zoom e ", false, false, false);

 

See the following link where @_gile explains different ways to do the zoom extents. 

https://forums.autodesk.com/t5/net/do-zoom-extents-in-c-net-autocad/td-p/7735556

0 Likes
Message 3 of 4

lmorse
Contributor
Contributor

Hi hippe013

Yes this does seem like the issue. I will find an alternative method.

 

Thank you.

0 Likes
Message 4 of 4

ActivistInvestor
Mentor
Mentor

Since your code is in a command method running in the document context there really isn't any need to use SendStringToExecute. It was intended for use by applications running in the application context, and executes asynchronously. That means the ZOOM Extents command will not execute until after your command method has ended.

 

From a CommandMethod running in the document context you should use the Editor's Command() method to run AutoCAD commands.

0 Likes