.NET

Reply
Contributor
Saumitra
Posts: 15
Registered: ‎09-12-2007
Message 1 of 11 (509 Views)

Drawing a Rectangle

509 Views, 10 Replies
11-30-2007 02:31 PM
Hi:

I am trying to draw a rectangle, given the 4 coordinates of the rectangle. I was basing my code on the lab tutorials for Cad 2007 where there is a description about drawing a circle.

bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite)
btrId = bt.Item(BlockTableRecord.ModelSpace)
btr = trans.GetObject(btrId, OpenMode.ForWrite)

rect = New Rectangle3d(ptTopLeft, ptTopRight, ptBottomLeft, ptBottomRight)
btr.AppendEntity(rect)
trans.AddNewlyCreatedDBObject(rect, True)

However, I am getting an error in the last 2 lines. The reason I presume is that Rectangle3d is a structure in the library. And the methods take DatabaseServices.Entity types as parameter. The code in the tutorial worked for circle/ellipse coz they are defined as classes.

Am I correct in my assumption and how can this be corrected? I have tried type-casting but that does not work either.

Thanks

Saumitra
*Tony Tanzillo
Message 2 of 11 (509 Views)

Re: Drawing a Rectangle

12-01-2007 07:19 AM in reply to: Saumitra
I'm not sure how you came up with the assumption
there's a 'Rectangle3d' class, but there's none.

There's no 'rectangle' object in AutoCAD, hence
there's no corresponding class.

The RECTANG command draws a 4-segment
polyline, not a 'RECTANGLE' object.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5789864@discussion.autodesk.com...
Hi:

I am trying to draw a rectangle, given the 4 coordinates of the rectangle. I was basing my code on the lab tutorials for Cad 2007 where there is a description about drawing a circle.

bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite)
btrId = bt.Item(BlockTableRecord.ModelSpace)
btr = trans.GetObject(btrId, OpenMode.ForWrite)

rect = New Rectangle3d(ptTopLeft, ptTopRight, ptBottomLeft, ptBottomRight)
btr.AppendEntity(rect)
trans.AddNewlyCreatedDBObject(rect, True)

However, I am getting an error in the last 2 lines. The reason I presume is that Rectangle3d is a structure in the library. And the methods take DatabaseServices.Entity types as parameter. The code in the tutorial worked for circle/ellipse coz they are defined as classes.

Am I correct in my assumption and how can this be corrected? I have tried type-casting but that does not work either.

Thanks

Saumitra
Contributor
Saumitra
Posts: 15
Registered: ‎09-12-2007
Message 3 of 11 (509 Views)

Re: Drawing a Rectangle

12-03-2007 06:26 AM in reply to: Saumitra
Well, I didnt assume that there was the Rectangle3d class.
There is a structure at
autodesk.AutoCAD.DatabaseServices.Rectangle3d

And also a structure called Rectangle...

I thought (given the name of the structure, it wasn't very presumptuous to give it a try) it would be the one to draw a rectangle. So, the only way to draw a rectangle is through the RECTANG command?

Saumitra
*Tony Tanzillo
Message 4 of 11 (509 Views)

Re: Drawing a Rectangle

12-03-2007 06:38 AM in reply to: Saumitra
>> Well, I didnt assume that there was the Rectangle3d class.

What I meant was that you assumed there was
a formal AutoCAD entity type 'rectangle', not that
there was a class for it.

As I said, there is no entity type corresponding
to a rectangle.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5790667@discussion.autodesk.com...
Well, I didnt assume that there was the Rectangle3d class.
There is a structure at
autodesk.AutoCAD.DatabaseServices.Rectangle3d

And also a structure called Rectangle...

I thought (given the name of the structure, it wasn't very presumptuous to give it a try) it would be the one to draw a rectangle. So, the only way to draw a rectangle is through the RECTANG command?

Saumitra
Contributor
Saumitra
Posts: 15
Registered: ‎09-12-2007
Message 5 of 11 (509 Views)

Re: Drawing a Rectangle

12-03-2007 06:59 AM in reply to: Saumitra
So, Tony is this a proper way to draw a rectangle then.

strCommand = "RECTANG" & ptBottomLeft.ToString & ptTopRight.ToString

acadDoc.SendStringToExecute(strCommand, True, False, True)

acadDoc is the current document. This one is not working. Am i on the wrong track and just try drawing a rectangle through 4 polylines?

Saumitra
*Tony Tanzillo
Message 6 of 11 (509 Views)

Re: Drawing a Rectangle

12-03-2007 07:07 AM in reply to: Saumitra
>> So, the only way to draw a rectangle is through
>> the RECTANG command?

I'll be happy to answer you, if you can first tell me if
you're new to AutoCAD and/or AutoCAD development.

I think you already know that this is something you
should make clear, so that others can give you an
answer in terms that you can understand, and not
make the mistake of presuming that you already
have a grasp of other more basic concepts that any
helpful answer would depend on.

The fact that you're asking the above question would
suggest that you've not done much AutoCAD work,
which is fine, so long as you make that clear at the
outset.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5790667@discussion.autodesk.com...
Well, I didnt assume that there was the Rectangle3d class.
There is a structure at
autodesk.AutoCAD.DatabaseServices.Rectangle3d

And also a structure called Rectangle...

I thought (given the name of the structure, it wasn't very presumptuous to give it a try) it would be the one to draw a rectangle. So, the only way to draw a rectangle is through the RECTANG command?

Saumitra
Contributor
Saumitra
Posts: 15
Registered: ‎09-12-2007
Message 7 of 11 (509 Views)

Re: Drawing a Rectangle

12-03-2007 07:40 AM in reply to: Saumitra
Well, I am extremely new to AutoCAD (let alone autocad development). Anyways, I have managed to create a rectangle through polylines. If for my knowledge purpose, you can answer what I asked earlier, it would be great. Next time onwards, I would specify what is required to mention in the email.
Distinguished Contributor
Mikko
Posts: 212
Registered: ‎10-03-2003
Message 8 of 11 (509 Views)

Re: Drawing a Rectangle

12-03-2007 09:50 AM in reply to: Saumitra
Here's a few lines that will draw a 3d solid rectangle that might be of help.

Dim db As Database = HostApplicationServices.WorkingDatabase
Using t As Transaction = db.TransactionManager.StartTransaction()
Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim bt As BlockTable = CType(t.GetObject(db.BlockTableId, OpenMode.ForRead, False), BlockTable)
Dim btr As BlockTableRecord = CType(t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, False), BlockTableRecord)
Dim r3d As New Solid3d
r3d.CreateBox(5.0, 2.0, 10.0)
btr.AppendEntity(r3d)
t.AddNewlyCreatedDBObject(r3d, True)
t.Commit()
End Using
*Tony Tanzillo
Message 9 of 11 (509 Views)

Re: Drawing a Rectangle

12-03-2007 12:27 PM in reply to: Saumitra
You can create polylines directly, without having to use the command line interface, but of course, that's a bit more involved than the 'easy' way (e.g., using the command line).

You use the Polyline class (call the constructor), and then set the properties to what you want and add the vertices, then add it to the database and to the block table record for the space you're drawing in.

Doing it that way does require some experience with the API, so if you are writing a real application that needs to draw rectangles, you can use the command line (or the ActiveX API) until you are able to use the more direct method.

Look at the code posted here and at the links posted here, for useful examples of using the API directly to create and work with AutoCAD objects.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5790800@discussion.autodesk.com...
Well, I am extremely new to AutoCAD (let alone autocad development). Anyways, I have managed to create a rectangle through polylines. If for my knowledge purpose, you can answer what I asked earlier, it would be great. Next time onwards, I would specify what is required to mention in the email.
Valued Contributor
nlvvch
Posts: 50
Registered: ‎03-21-2006
Message 10 of 11 (509 Views)

Re: Drawing a Rectangle

12-03-2007 09:11 PM in reply to: Saumitra
Hi, soumitra

there is no rectangle class, there is only line, polyline and lwpolyline only. so u try it as polyline are lwpolyline


venkatesh
venkatesh.chitimilla@regencyinfotech.biz
regency infotech
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

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