.NET

Reply
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 1 of 10 (392 Views)

Automatically Arrange Blocks

392 Views, 9 Replies
03-08-2012 05:52 AM

Hey everyone,

 

I'm wondering if anyone has an example of, or knows where to start with this. I've created a basic GUI which can import a specific block "x" amount of times into a DWG. At this point, I want to automatically arrange these blocks within either a selection set or rectangle (essentially between four points, like a box) in a linear fashion. So if i have a rectangle that is 4 units wide and 2 units high, and each block is 1 unit by 1 unit, i'd like to have 2 rows, each comprised of four blocks. I know AutoCAD already has an array command that can accomplish something similar, however what i'm trying to do will need some serious tweaking down the line and i'm looking for somewhere to start. Hopefully someone may have encountered this before or has some idea.

 

Cheers

Vince

Valued Contributor
FFlix
Posts: 95
Registered: ‎11-15-2011
Message 2 of 10 (382 Views)

Re: Automatically Arrange Blocks

03-08-2012 06:45 AM in reply to: vince1327

hi vince

 

one idea would be to:

(i think you already have) create the block definition; and

start a row-loop, e.g. from 0 to 1; and

a column-loop, e.g. from 0 to 3; and

insert the 1st block ref in each row via its constructor parameter at pt_column0(row); and

the following block refs in each respective row via blockref.gettransformedcopy(matrix3d.displacement(vector parameter dependent on column spacing * column-loop counter)).

 

hope that's what you're after

felix

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 3 of 10 (380 Views)

Re: Automatically Arrange Blocks

03-08-2012 06:47 AM in reply to: FFlix

Hey Felix,

 

I think that's what i'm after. Do you know of any examples posted anywhere of this or something similar?

 

Cheers

 

Valued Contributor
FFlix
Posts: 95
Registered: ‎11-15-2011
Message 4 of 10 (377 Views)

Re: Automatically Arrange Blocks

03-08-2012 06:52 AM in reply to: vince1327

unfortunately no (although i've never actually looked for anything published in this regard), sorry

Valued Mentor
fieldguy
Posts: 382
Registered: ‎03-31-2005
Message 5 of 10 (369 Views)

Re: Automatically Arrange Blocks

03-08-2012 08:08 AM in reply to: vince1327

I am working on the same thing sort of.  I need to create a poster of all blocks in a library.  At the same time, it will serve as a QC to make sure all blocks are defined at the same relative size, insertion is the middle, etc.

 

Assume you have a rectangle 4x2 and the top left corner is @ 100,100.  The 1st block will be inserted at 101, 99, 2nd @ 102, 99, 3 @ 103,99, 4 @ 104, 99.  After 4 is placed, you need to change Y to 99.  Block 1 row 2 is 101, 98, etc.

 

Thats how I am doing it except I am using myblockref.GeometricExtents().  I add 1/2 of the difference between maxpoint.x - minpoint.x + 5%.  My 1st insert is forced to 1000,1000.  I then use 1/2 of the X extents of the block plus a space factor (5%) to determine the insert of block 2.  My ultimate goal is to have the user pick a page size and a start point and use that as "the box".  I don't have much coded yet, but attached is where I am at.

 

 

 

Valued Mentor
fieldguy
Posts: 382
Registered: ‎03-31-2005
Message 6 of 10 (367 Views)

Re: Automatically Arrange Blocks

03-08-2012 08:10 AM in reply to: fieldguy

Oops - coffe hasn't kicked in yet.

 After 4 is placed, you need to change Y to 98.  Block 1 row 2 is 101, 98, etc.

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 7 of 10 (350 Views)

Re: Automatically Arrange Blocks

03-08-2012 11:18 AM in reply to: fieldguy

Awesome, this helps a bunch...so my next question has been bugging me for a while. I need to select an area in which to auto-arrange my blocks. Is there any selection tool that I can code/call from code that will allow me to select an area and then return it's co-ordinates and potentially area so that I can create points in which to place my blocks? 

 

Cheers!

Valued Mentor
fieldguy
Posts: 382
Registered: ‎03-31-2005
Message 8 of 10 (341 Views)

Re: Automatically Arrange Blocks

03-08-2012 12:33 PM in reply to: vince1327

This code (attached) prompts for a point. 

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 9 of 10 (331 Views)

Re: Automatically Arrange Blocks

03-08-2012 01:36 PM in reply to: fieldguy

Thanks fieldguy.

 

This is where i'm at so far. The problem with this is, although i can pick four points ,they are all of varying X and Y values, so it will be hard to calculate area. Is there a way I can do this as if i was creating a rectangle for instance? That way I could visualize the lines while selecting the points, but still obtain enough information to determine area?

 

Cheers

Vince

 

namespace Corners
{
public class Corners
{

[CommandMethod("Corners", CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]
public void GeWindowPointSelection()
{

Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
List<Point3d> points = new List<Point3d>();
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
try
{
PromptPointOptions ppo = new PromptPointOptions("\n\tSpecify a first corner: ");
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK) return;

PromptPointOptions ppo1 = new PromptPointOptions("\n\tSpecify a second corner: ");
PromptPointResult ppr1 = ed.GetPoint(ppo1);
if (ppr1.Status != PromptStatus.OK) return;

PromptPointOptions ppo2 = new PromptPointOptions("\n\tSpecify a third corner: ");
PromptPointResult ppr2 = ed.GetPoint(ppo2);
if (ppr2.Status != PromptStatus.OK) return;

PromptPointOptions ppo3 = new PromptPointOptions("\n\tSpecify a fourth corner: ");
PromptPointResult ppr3 = ed.GetPoint(ppo3);
if (ppr3.Status != PromptStatus.OK) return;



Point3d pt1 = ppr.Value;
Point3d pt2 = ppr1.Value;
Point3d pt3 = ppr2.Value;
Point3d pt4 = ppr3.Value;


double X1 = Convert.ToDouble(pt1.X);
double Y1 = Convert.ToDouble(pt1.Y);

double X2 = Convert.ToDouble(pt2.X);
double Y2 = Convert.ToDouble(pt2.Y);

double X3 = Convert.ToDouble(pt3.X);
double Y3 = Convert.ToDouble(pt3.Y);

double X4 = Convert.ToDouble(pt4.X);
double Y4 = Convert.ToDouble(pt4.Y);


/*
if (pt1.X == pt2.X || pt1.Y == pt2.Y)
{
ed.WriteMessage("\nInvalid point specification");
return;
}
*
* */


BlockTableRecord acadBTR = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
MText acadMText1 = new MText();
MText acadMText2 = new MText();
MText acadMText3 = new MText();
MText acadMText4 = new MText();
MText acadMText5 = new MText();
MText acadMText6 = new MText();
MText acadMText7 = new MText();
MText acadMText8 = new MText();



acadMText1.Location = new Autodesk.AutoCAD.Geometry.Point3d(12, 5, 0);
acadMText2.Location = new Autodesk.AutoCAD.Geometry.Point3d(12, 7, 0);
acadMText3.Location = new Autodesk.AutoCAD.Geometry.Point3d(12, 9, 0);
acadMText4.Location = new Autodesk.AutoCAD.Geometry.Point3d(12, 11, 0);
acadMText5.Location = new Autodesk.AutoCAD.Geometry.Point3d(12, 13, 0);
acadMText6.Location = new Autodesk.AutoCAD.Geometry.Point3d(12, 15, 0);
acadMText7.Location = new Autodesk.AutoCAD.Geometry.Point3d(12, 17, 0);
acadMText8.Location = new Autodesk.AutoCAD.Geometry.Point3d(12, 19, 0);


acadMText1.Contents = "Double X1 = " + X1;
acadMText2.Contents = "Double X2 = " + X2;
acadMText3.Contents = "Double X3 = " + X3;
acadMText4.Contents = "Double X4 = " + X4;
acadMText5.Contents = "Double Y1 = " + Y1;
acadMText6.Contents = "Double Y2 = " + Y2;
acadMText7.Contents = "Double Y3 = " + Y3;
acadMText8.Contents = "Double Y4 = " + Y4;


acadBTR.AppendEntity(acadMText1);
acadBTR.AppendEntity(acadMText2);
acadBTR.AppendEntity(acadMText3);
acadBTR.AppendEntity(acadMText4);
acadBTR.AppendEntity(acadMText5);
acadBTR.AppendEntity(acadMText6);
acadBTR.AppendEntity(acadMText7);
acadBTR.AppendEntity(acadMText8);


tr.Commit();
ed.Regen();
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
}
}
}


}
}

Valued Mentor
fieldguy
Posts: 382
Registered: ‎03-31-2005
Message 10 of 10 (321 Views)

Re: Automatically Arrange Blocks

03-08-2012 03:22 PM in reply to: vince1327

No problem.  It's always good to give something back to this forum.

 

Maybe you could use a jig? - something I have never used.  Keep searching!  Maybe this: http://www.theswamp.org/index.php?topic=30252.0

 

Good luck!

 

   

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.