.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Autocad 2012 - Get objects inside layer

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
Seegee
2941 Views, 10 Replies

Autocad 2012 - Get objects inside layer

Hey guys, quick question.

 

I am using Autocad 2012 with the API provided. I am developing in c#.

 

What I am trying to do is select a certain layer, and "detect" all rectangles / squares in that layer. Ultimateley, I would like to be able to draw inside of all of those rectangles that I have "detected" (using their coordinates).

So far, I am using the LayerTable class along with GetObjects to associate layers with objects, like so:

 

            String layerNames = "";

            foreach (ObjectId layer in layers)
            {
                LayerTableRecord layerTableRec;
                layerTableRec = acTrans.GetObject(layer, OpenMode.ForRead) as LayerTableRecord;
                layerNames += layerTableRec.Name+"\n";
            }

 

 

LayerTable layers; layers = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;I can't seem to figure out where to go from here though. How to select just one layer, and then detect shapes inside of it. Can someone point me in the correct direction, in terms of what classes / methods to look into?

 

Thanks!

 

Tags (3)
10 REPLIES 10
Message 2 of 11
Keith.Brown
in reply to: Seegee

You could just use a selection filter.  The code below is a direct copy paste from the api help file.  It will select all blue circles on layer 0.

 

http://docs.autodesk.com/ACD/2014/ENU/index.html?url=files/GUID-BE8B5EE4-9B5A-4D1F-B2D8-7DC013BFC6C0...

 

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
 
[CommandMethod("FilterBlueCircleOnLayer0")]
public static void FilterBlueCircleOnLayer0()
{
  // Get the current document editor
  Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
 
  // Create a TypedValue array to define the filter criteria
  TypedValue[] acTypValAr = new TypedValue[3];
  acTypValAr.SetValue(new TypedValue((int)DxfCode.Color, 5), 0);
  acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "CIRCLE"), 1);
  acTypValAr.SetValue(new TypedValue((int)DxfCode.LayerName, "0"), 2);
 
  // Assign the filter criteria to a SelectionFilter object
  SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);
 
  // Request for objects to be selected in the drawing area
  PromptSelectionResult acSSPrompt;
  acSSPrompt = acDocEd.GetSelection(acSelFtr);
 
  // If the prompt status is OK, objects were selected
  if (acSSPrompt.Status == PromptStatus.OK)
  {
      SelectionSet acSSet = acSSPrompt.Value;
 
      Application.ShowAlertDialog("Number of objects selected: " +
                                  acSSet.Count.ToString());
  }
  else
  {
      Application.ShowAlertDialog("Number of objects selected: 0");
  }
}

 

Message 3 of 11
Seegee
in reply to: Keith.Brown

Ahh ok. That would select them in Autocad though.

How would I go about getting dimensions / coordinates of each individual object?
Message 4 of 11
Keith.Brown
in reply to: Seegee

Ok...  I threw this example together real fast but i did check it and it worked.  It gets all objects in model space and checks to see if it is a polyline.  It will then open the polyline for read and check to see if it is on the correct layer.  It will then check to see if the polyline is rectangular by verifying it has 4 vertices and if all 4 vertices are perpindicular to each other.  There might be a better/faster way to do it but this is what I came up with.

 

[CommandMethod("GetAllRectangles")]
public static void GetCircles()
{
    Document document = Application.DocumentManager.MdiActiveDocument;
    Database database = document.Database;
    Editor editor = document.Editor;
    RXClass rxClassPoly = RXClass.GetClass(typeof (Polyline));
    var count = 0;
	
    using (Transaction transaction = document.TransactionManager.StartOpenCloseTransaction())
    {
        var blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForWrite, false) as BlockTable;
	var blocktableRecord = transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead, false) as BlockTableRecord;
	foreach (ObjectId objectId in blocktableRecord)
	{
	    if (objectId.ObjectClass == rxClassPoly)
	    {
	        var poly = transaction.GetObject(objectId, OpenMode.ForRead) as Polyline;
		if (poly.Layer == "0" && poly != null)
		{
		    if (poly.NumberOfVertices == 4)
		    {
		        var line0 = poly.GetLineSegmentAt(0);
		        var line1 = poly.GetLineSegmentAt(1);
		        var line2 = poly.GetLineSegmentAt(2);
		        var line3 = poly.GetLineSegmentAt(3);

		        if (line0.IsPerpendicularTo(line1) && line1.IsPerpendicularTo(line2) &&
                            line2.IsPerpendicularTo(line3) && line3.IsPerpendicularTo(line0))
		        {
		            count = count + 1;

			    // Put all your rectangle processing code here.

		        }
	             }
	        }
            }
        }
        transaction.Commit();
    }
    editor.WriteMessage(string.Format("\r\nFound {0} rectangles!", count));
}

 

 

Message 5 of 11
Seegee
in reply to: Keith.Brown

Hi, sorry for the late reply!

 

I just tested out that code, and it doesn't seem to be working. I draw a rectangle with a polyline, and then I run GetAllRectangles, but it just outputs Found 0 rectangles every time.

Message 6 of 11
JONTHEPOPE
in reply to: Seegee

since my programming is limited I would use the qselect method.

are better right click object and find select similar

Message 7 of 11
Seegee
in reply to: Keith.Brown

It seems that poly.NumberOfVertices is equal to 5 for some reason, when I draw a rectangle.

Message 8 of 11
JONTHEPOPE
in reply to: Seegee

reset cad.pngSounds like a variable problem

if you want reset autocad in you window button

 

 

Message 9 of 11
Keith.Brown
in reply to: Seegee


@Seegee wrote:

Hi, sorry for the late reply!

 

I just tested out that code, and it doesn't seem to be working. I draw a rectangle with a polyline, and then I run GetAllRectangles, but it just outputs Found 0 rectangles every time.


Hi,

 

Are you drawing a rectangle or a polyline with 4 sides?  If you are drawing a polyline with 4 sides make sure that you close the polyline instead of trying to mate the last line with the first line.

 

I ran this code many times and it worked for me everytime when i created a rectangle using the RECTANG command.

Message 10 of 11
Seegee
in reply to: Keith.Brown

Got it working, I was clicking with last line. Thanks!
Message 11 of 11
Keith.Brown
in reply to: Seegee

Don't forget to make the post that was the solution for you so others can find the answer quickly.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost