Jump to content

How to find blocks in a dwg


Telefisch

Recommended Posts

Hello forum...

I'm beginner in autoCAD and I'm writing a little Test-Program based on C#.

 

Im trying to get the Blocks and their positions out of a drawing.

 

Til now I'm able to get the names of the blocks but only one time.

There are two identical blocks in the drawing and I'll have to get the positions of each, also the attribute-values.

 

culd somebody help me solving this problem?

 

If there are VBA-Codesnippets, this would perhaps help too.

 

many thanks

Telefisch

Link to comment
Share on other sites

It's an Autocad command. If you need code, someone will probably be along soon to help.

or try here ; a good source for programming, you'll need to register, but it's free.

Link to comment
Share on other sites

Perhaps this:

 

(defun c:blocext (/ fpath)
   (setvar "cmdecho" 0)
   (setq fpath (getstring t "\nSpecify Filepath to Save to: "))
   (command "-eattext" "C" "Y" "Y" "" "Xls" fpath)
   (setvar "cmdecho" 1)
   (princ)
) ; end blocext

This will write all block information to an Excel Doc, and save it in the specified location.

 

Specify you location like this:

 

"C:\XXX\XXX\filename"

 

And the LISP will create an Excel doc called "filename", and save it to "C:\XXX\XXX"

 

Or, if you are using the same location every time:

 

(defun c:blocext2 (/ fname fpath)
   (setvar "cmdecho" 0)
   (setq fname (getstring t "\nSpecify Filename: "))
   (setq fpath (strcat "[b][color=Red]C:\\Users\\[/color][/b]" fname)) ; <<---<< Change this line for a different Location
   (command "-eattext" "C" "Y" "Y" "" "Xls" fpath)
   (setvar "cmdecho" 1)
   (princ)
) ; end blocext2

Let me know if this helps at all :P

Link to comment
Share on other sites

first, many thanks to lee mac and lpseifert...

 

I was searching for a way to solve this with an activex-control of Open Design Alliance.

Because its similar in many points, I hoped that I can find a little help here.

I'm still thinking this DWGdirectX-Control isn't very different to ACAD-Api so I think my way solving this problem could be helpfull to others...

 

So, thats it in C#:

 

try
           {
               foreach (IAcadEntity blk in oDoc.ModelSpace) //iterate thru all Entities in Modelspace
               {

                   if (blk.ObjectName == "AcDbBlockReference") //If the Entity is a Block Reference, go on.
                   {
                       IAcadBlockReference blkRef = (IAcadBlockReference)blk; //Cast Entity to BlockReference
                       if (blkRef.EffectiveName.Substring(0, 4).ToLower() == "pdms") //just filter the needed Blocks
                       {
                           //List Name of Block in Listview
                           strItem[0] = "Name";
                           strItem[0] = blkRef.EffectiveName;
                           item = new ListViewItem(strItem);
                           lvw.Items.Add(item);
                           
                           //Get InsertionPoint and Cast it to double-array
                           double[] dblPosition = (double[])blkRef.InsertionPoint;
                           
                           //insert the Position in Listview
                           strItem[0] = "Position X";
                           strItem[1] = Convert.ToString(dblPosition[0]);
                           item = new ListViewItem(strItem);
                           lvw.Items.Add(item);

                           strItem[0] = "Position Y";
                           strItem[1] = Convert.ToString(dblPosition[1]);
                           item = new ListViewItem(strItem);
                           lvw.Items.Add(item);

                           strItem[0] = "Position Z";
                           strItem[1] = Convert.ToString(dblPosition[2]);
                           item = new ListViewItem(strItem);
                           lvw.Items.Add(item);
...

HTH,

Telefisch

Link to comment
Share on other sites

  • 1 month later...

In VBA you could use this Code:

 

Public Sub BlocksSearch ()

Dim Block as AcadBlock

For Each Block in ThisDrawing.Blocks

MsgBox "The Block Name is: " & Block.Name & " The Insertion Point is: " Block.InsertionPoint"

Next Block

Link to comment
Share on other sites

The Vba and C are very similar using Zeusovsky above you would search for all blocks then make a list then sort the list by block name before outputting the result.

 

rather than foreach

 

Dim SS As AcadSelectionSet
Dim Count, Cntr As Integer
Dim FilterDXFCode(0) As Integer
Dim FilterDXFVal(0) As Variant
Dim basepnt, pt1, pt2, pt3 As Variant
FilterDXFCode(0) = 0
FilterDXFVal(0) = "INSERT"
Set SS = ThisDrawing.SelectionSets.Add("MyBLOCKS")
SS.Select acSelectionSetAll, , , FilterDXFCode, FilterDXFVal
For Cntr = 0 To SS.Count - 1
BLOCK_NAME=SS.Item(Cntr).Name



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...