Draw rectangle with c#

Draw rectangle with c#

Anonymous
Not applicable
7,423 Views
2 Replies
Message 1 of 3

Draw rectangle with c#

Anonymous
Not applicable

Hi guys, i'm searching haw to draw a rectangle with this code but can't find anithing.

After the definition of the points of the polyline i don't know how to send the command to draw the rectangle.

 

 

public void Trace(int Puissance, string Orientation, string Layer)
    {
     
      try
      {
        object[] objArray = new object[2];
        this.acDoc.ActiveLayer = this.acDoc.Layers.Item((object) Layer);
        new GetPointCoord.GetPointCoord().GetPoint(this.acDoc, ref objArray[0], ref objArray[1]);
        double num1 = this.RecupEchelle(this.acDoc);
        double num2;
        double num3;
        switch (Orientation)
        {
          case "Verticale":
            num2 = num1 * (Math.Log((double) Puissance) / (Math.Log(10.0) * 6.58));
            num3 = num1 * (Math.Log((double) Puissance) / (Math.Log(10.0) * 3.15));
            break;
          case "Horizontale":
            num3 = num1 * (Math.Log((double) Puissance) / (Math.Log(10.0) * 6.58));
            num2 = num1 * (Math.Log((double) Puissance) / (Math.Log(10.0) * 3.15));
            break;
          default:
            num3 = num1 * (Math.Log((double) Puissance) / (Math.Log(10.0) * 6.58));
            num2 = num1 * (Math.Log((double) Puissance) / (Math.Log(10.0) * 3.15));
            break;
        }
        this.acDoc.ModelSpace.AddLightWeightPolyline((object) new double[10]
        {
          (double) objArray[0] - num2 / 2.0,
          (double) objArray[1] + num3 / 2.0,
          (double) objArray[0] + num2 / 2.0,
          (double) objArray[1] + num3 / 2.0,
          (double) objArray[0] + num2 / 2.0,
          (double) objArray[1] - num3 / 2.0,
          (double) objArray[0] - num2 / 2.0,
          (double) objArray[1] - num3 / 2.0,
          (double) objArray[0] - num2 / 2.0,
          (double) objArray[1] + num3 / 2.0
        }).Closed = true;
        acDoc.Application.Update();
      }
      catch (System.Exception ex)
      {
        int num = (int) MessageBox.Show(ex.Message);
      }
       
    }

 

 

 

 

0 Likes
7,424 Views
2 Replies
Replies (2)
Message 2 of 3

BKSpurgeon
Collaborator
Collaborator

I cannot really work out what you are doing:

 

new GetPointCoord.GetPointCoord().GetPoint(this.acDoc, ref objArray[0], ref objArray[1]);

 

I could not find that class nor method in my managedClass Reference Guide nor could I find this either:

 

double num1 = this.RecupEchelle(this.acDoc);

 

What is RecupEchlle? Never heard of it. 

 

Personally I wouldn't write it like that. But let's get to the real issue: how to add things to the model space?

 

 

Generally these are done within a "transaction". What is a "transaction"? Think of it like a special place in time where goods are bought or sold in a market place. Similarly, within autocad, it is a special place where autocad entities are manipulated. Then you have the concept of a block table record. What is a blocktablerecord? It is just a basket of goods. Or rather  a basket of autocad entities. If you want to add something to a model space, then you must add the line, rectangle etc. to the modelspace "blocktable record" or to the modelspace basket. Once you have added it to the basket you must add it to the transaction - because you are "purchasing" those goods. And once you add it to the transaction, you must commit the transaction.

 

 

You can adapt the below code to your needs - i do not understand your code so I am not going to attempt to rewrite it.

 

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("AddLightweightPolyline")]
public static void AddLightweightPolyline()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Block table for read
        BlockTable acBlkTbl;
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                        OpenMode.ForRead) as BlockTable;

        // Open the Block table record Model space for write
        BlockTableRecord acBlkTblRec;
        acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                        OpenMode.ForWrite) as BlockTableRecord;

        // Create a polyline with two segments (3 points)
        using (Polyline acPoly = new Polyline())
        {
            acPoly.AddVertexAt(0, new Point2d(2, 4), 0, 0, 0);
            acPoly.AddVertexAt(1, new Point2d(4, 2), 0, 0, 0);
            acPoly.AddVertexAt(2, new Point2d(6, 4), 0, 0, 0);

            // Add the new object to the block table record and the transaction
            acBlkTblRec.AppendEntity(acPoly);
            acTrans.AddNewlyCreatedDBObject(acPoly, true);
        }

        // Save the new object to the database
        acTrans.Commit();
    }
}

good luck and i hope this helps.

 

regards

BK

 

 

0 Likes
Message 3 of 3

_gile
Consultant
Consultant

Hi,

 

See my reply here.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes