.NET

Reply
Contributor
CodeBug
Posts: 18
Registered: ‎07-17-2012
Message 1 of 6 (510 Views)
Accepted Solution

How to Initialize AcadEntity array Object with NULL value in C#.Net ?

510 Views, 5 Replies
08-06-2012 10:39 PM

Hi all,

 

I would like to know how to initialize the AcadEntity type of array variable with an ACADEntity object

 

My code is as below,

 

AcadEntity[] SelEntList;

AcadEntity MyEnt;

 

 

SelEntList[0] = MyEnt;

 

I am getting an error as below 

Use of unassigned local variable 'MyEnt'

 

In case of a string array, I would be doing MyStr[0] = "" and it works but how to do this w.r.t. AcadEntity array ?

Hi, 

 

You have to intialize your variables after (or while) you declare them.

 

AcadEntity[] SelEntList = new AcadEntity[arrayLength]; // you have to specify the array size when initilzing it.

AcadEntity MyEnt = null;

 

SelEntList[0] = MyEnt;

 

Have a look at the System.Collections.Generic.List<T> which has some intersting features the array don't have.

 

*Expert Elite*
_gile
Posts: 2,086
Registered: ‎04-29-2006
Message 2 of 6 (503 Views)

Re : How to Initialize AcadEntity array Object with NULL value in C#.Net ?

08-06-2012 11:43 PM in reply to: CodeBug

Hi, 

 

You have to intialize your variables after (or while) you declare them.

 

AcadEntity[] SelEntList = new AcadEntity[arrayLength]; // you have to specify the array size when initilzing it.

AcadEntity MyEnt = null;

 

SelEntList[0] = MyEnt;

 

Have a look at the System.Collections.Generic.List<T> which has some intersting features the array don't have.

 

Gilles Chanteau
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 6 (492 Views)

Re: How to Initialize AcadEntity array Object with NULL value in C#.Net ?

08-07-2012 03:55 AM in reply to: CodeBug

Try this code that completely based on docs example^

 

     static Point2d PolarPoints(Point2d pPt, double dAng, double dDist)
        {
            return new Point2d(pPt.X + dDist * Math.Cos(dAng),
                               pPt.Y + dDist * Math.Sin(dAng));
        }

        // create grid using rectangular array
        [CommandMethod("sqv")]
        public static void TestFillSquares()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            PromptPointOptions ppo = new PromptPointOptions("\nPick a lower left corner: ");
            PromptPointResult res = ed.GetPoint(ppo);
            if (res.Status != PromptStatus.OK) return;
            Point3d p1 = res.Value;
            PromptCornerOptions pko = new PromptCornerOptions("\nPick opposite corner: ", p1);
            res = ed.GetCorner(pko);
            if (res.Status != PromptStatus.OK) return;
            Point3d p2 = res.Value;
            //start a transaction
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                // create polyline to make the shape 
                Polyline poly = new Polyline(4);
                poly.AddVertexAt(0, new Point2d(p1.X, p1.Y), 0, 0, 0);
                poly.AddVertexAt(1, new Point2d(p2.X, p1.Y), 0, 0, 0);
                poly.AddVertexAt(2, new Point2d(p2.X, p2.Y), 0, 0, 0);
                poly.AddVertexAt(3, new Point2d(p1.X, p2.Y), 0, 0, 0);
                poly.Closed = true;

                btr.AppendEntity(poly);
                tr.AddNewlyCreatedDBObject(poly, true);
               
                double leg = Math.Abs(p2.X - p1.X);
                double wid = Math.Abs(p2.Y - p1.Y);
                // Create a rectangular array with 5 rows and 8 columns
                int nRows = 5;
                int nColumns = 8;
                double disrows = leg / nColumns;
                double discols = wid / nRows;
                // create polyline to make the shape 
                Polyline npoly = new Polyline(4);
                npoly.AddVertexAt(0, new Point2d(p1.X, p1.Y), 0, 0, 0);
                npoly.AddVertexAt(1, new Point2d(p1.X + disrows, p1.Y), 0, 0, 0);
                npoly.AddVertexAt(2, new Point2d(p1.X + disrows, p1.Y + discols), 0, 0, 0);
                npoly.AddVertexAt(3, new Point2d(p1.X, p1.Y + discols), 0, 0, 0);
                npoly.Closed = true;
                npoly.ColorIndex = 1;
                btr.AppendEntity(npoly);
                tr.AddNewlyCreatedDBObject(npoly, true);



                // Set the row and column offsets along with the base array angle
                double dRowOffset = discols;
                double dColumnOffset = disrows;
                double dArrayAng = 0;

                // Get the angle from X for the current UCS 
                Matrix3d curUCSMatrix = ed.CurrentUserCoordinateSystem;
                CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
                Vector2d acVec2dAng = new Vector2d(curUCS.Xaxis.X,
                                                   curUCS.Xaxis.Y);

                // If the UCS is rotated, adjust the array angle accordingly
                dArrayAng = dArrayAng + acVec2dAng.Angle;

                // Use the upper-left corner of the objects extents for the array base point
                Extents3d acExts = npoly.Bounds.GetValueOrDefault();
                Point2d acPt2dArrayBase = new Point2d(acExts.MinPoint.X,
                                                      acExts.MaxPoint.Y);

                // Track the objects created for each column
                DBObjectCollection acDBObjCollCols = new DBObjectCollection();
                acDBObjCollCols.Add(npoly);

                // Create the number of objects for the first column
                int nColumnsCount = 1;
                while (nColumns > nColumnsCount)
                {
                    Entity acEntClone = npoly.Clone() as Entity;
                    acDBObjCollCols.Add(acEntClone);

                    // Caclucate the new point for the copied object (move)
                    Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,
                                                   dArrayAng,
                                                   dColumnOffset * nColumnsCount);

                    Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);
                    Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
                    acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));

                    btr.AppendEntity(acEntClone);
                    tr.AddNewlyCreatedDBObject(acEntClone, true);

                    nColumnsCount = nColumnsCount + 1;
                }

                // Set a value in radians for 90 degrees
                double dAng = Math.PI / 2;

                // Track the objects created for each row and column
                DBObjectCollection acDBObjCollLvls = new DBObjectCollection();

                foreach (DBObject acObj in acDBObjCollCols)
                {
                    acDBObjCollLvls.Add(acObj);
                }

                // Create the number of objects for each row
                foreach (Entity acEnt in acDBObjCollCols)
                {
                    int nRowsCount = 1;

                    while (nRows > nRowsCount)
                    {
                        Entity acEntClone = acEnt.Clone() as Entity;
                        acDBObjCollLvls.Add(acEntClone);

                        // Caclucate the new point for the copied object (move)
                        Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,
                                                       dArrayAng + dAng,
                                                       dRowOffset * nRowsCount);

                        Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);
                        Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
                        acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));

                        btr.AppendEntity(acEntClone);
                        tr.AddNewlyCreatedDBObject(acEntClone, true);

                        nRowsCount = nRowsCount + 1;
                    }
                }


                tr.Commit();
            }
        }

 

 

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Contributor
CodeBug
Posts: 18
Registered: ‎07-17-2012
Message 4 of 6 (486 Views)

Re : How to Initialize AcadEntity array Object with NULL value in C#.Net ?

08-07-2012 04:02 AM in reply to: _gile

Thanks _gile! Thats the missing !

Contributor
CodeBug
Posts: 18
Registered: ‎07-17-2012
Message 5 of 6 (485 Views)

Re: How to Initialize AcadEntity array Object with NULL value in C#.Net ?

08-07-2012 04:03 AM in reply to: Hallex

Thanks Hallex !! Refered your code in my attempt.. 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 6 of 6 (482 Views)

Re: How to Initialize AcadEntity array Object with NULL value in C#.Net ?

08-07-2012 04:15 AM in reply to: CodeBug

You're welcome

Happy coding :smileyhappy:

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.