AUTOCAD .NET BLOCK COUNT TABLE

AUTOCAD .NET BLOCK COUNT TABLE

cihanbaki2147
Contributor Contributor
3,368 Views
12 Replies
Message 1 of 13

AUTOCAD .NET BLOCK COUNT TABLE

cihanbaki2147
Contributor
Contributor
AUTOCAD .NET  BLOCK  COUT  LİST  İNSERT  TABLE  CEL. 

 

Document doc = AcAp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;


string[] blokcneme = new string[3];

blokcneme[0] = "name1.dwg";
blokcneme[1] = "name2.dwg";
blokcneme[2] = "name3.dwg";


TypedValue[] filter =

{


new TypedValue( (int)DxfCode.Operator, "<or"),
new TypedValue((int)DxfCode.Start, "INSERT"),
new TypedValue( (int)DxfCode.Operator,"<and"),
new TypedValue((int)DxfCode.BlockName,blokcneme[0]),
new TypedValue((int)DxfCode.Operator, "and>" ),
new TypedValue( (int)DxfCode.Operator,"<and"),
new TypedValue((int)DxfCode.BlockName,blokcneme[1]),
new TypedValue((int)DxfCode.Operator, "and>" ),
new TypedValue( (int)DxfCode.Operator,"<and"),
new TypedValue((int)DxfCode.BlockName,blokcneme[2]),
new TypedValue((int)DxfCode.Operator, "and>" ),
new TypedValue( (int)DxfCode.Operator,"or>")


};

 


// Prompt the user for a selection
PromptSelectionResult psr = ed.GetSelection(new SelectionFilter(filter));
if (psr.Status != PromptStatus.OK) return;

SelectionSet ss = psr.Value;

string blokname = "";
int cout= 0;
using (Transaction tr = db.TransactionManager.StartTransaction())
{

foreach (ObjectId brId in psr.Value.GetObjectIds())
{

BlockReference br = (BlockReference)tr.GetObject(brId, OpenMode.ForRead);

 

cout = psr.Value.Count;
blokname += br.Name.ToString()+"cout"+cout.ToString();

}

ed.WriteMessage( blokname);

 

 

tr.Commit();

 

 

 


}

0 Likes
Accepted solutions (1)
3,369 Views
12 Replies
Replies (12)
Message 2 of 13

_gile
Consultant
Consultant

Hi,

 

It seems like you want to run before you know how to walk.

You should learn the basics of the AutoCAD .NET API with simpler tasks.

for example, concerning selection sets, you should read this topic and more accurately about selection sets this one.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 13

cihanbaki2147
Contributor
Contributor

I want to list the mores and names of the blocks in the drawing so I know the autocad selection sessions and filtering processes.

0 Likes
Message 4 of 13

_gile
Consultant
Consultant
Accepted solution

Did you try your code to select blocks?

It seems to me it can't work. You confuse block name and block file name (with .dwg extension) which won't work in a selection filter,  and you selection filter contains many superfluous expressions.

You can try the following code.

 

        [CommandMethod("BLKCNT")]
        public static void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var filter = new SelectionFilter(new[] {
                new TypedValue(0, "INSERT"),
                new TypedValue(2, "name1,name2,name3") });
            var psr = ed.GetSelection(filter);
            if (psr.Status != PromptStatus.OK)
                return;

                var ppr = ed.GetPoint("\nSpecify the table insertion point: ");
                if (ppr.Status != PromptStatus.OK)
                    return;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var blocks = psr.Value
                    .Cast<SelectedObject>()
                    .Select(so => (BlockReference)tr.GetObject(so.ObjectId, OpenMode.ForRead))
                    .GroupBy(br => br.Name)
                    .ToDictionary(gr => gr.Key, gr => gr.ToArray());
                double rowHeight = 9.0;
                double colWidth = 50.0;
                var table = new Table();
                table.TableStyle = db.Tablestyle;
                table.Cells[0, -1].Style = "_TITLE";
                table.Columns[0].Width = colWidth;
                table.InsertRows(1, rowHeight, blocks.Count + 1);
                table.InsertColumns(1, colWidth, 2);
                table.Cells[0, 0].Value = "BLOCK DATA";
                table.Cells[1, 0].Value = "PREVIEW";
                table.Cells[1, 1].Value = "BLOCK NAME";
                table.Cells[1, 2].Value = "COUNT";
                int row = 2;
                foreach (var entry in blocks)
                {
                    table.Cells[row, 0].BlockTableRecordId = entry.Value[0].BlockTableRecord;
                    table.Cells[row, 1].Value = entry.Key;
                    table.Cells[row, 2].Value = entry.Value.Length;
                    row++;
                }
                table.Position = ppr.Value;
                table.TransformBy(ed.CurrentUserCoordinateSystem);
                var cSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                cSpace.AppendEntity(table);
                tr.AddNewlyCreatedDBObject(table, true);
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 13

fsl88
Contributor
Contributor

Thanks for this. Im having an error in 

 

var blocks = psr.Value
.Cast<SelectedObject>()
.Select(so => (BlockReference)tr.GetObject(so.ObjectId, OpenMode.ForRead))
.GroupBy(br => br.Name)
.ToDictionary(gr => gr.Key, gr => gr.ToArray());

 

 

 

Error 4 'Autodesk.AutoCAD.EditorInput.SelectionSet' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type 'Autodesk.AutoCAD.EditorInput.SelectionSet' could be found (are you missing a using directive or an assembly reference?)

0 Likes
Message 6 of 13

_gile
Consultant
Consultant

Hi,

Try like this:

var blocks = psr.Value.GetObjectIds()
    .Select(id => (BlockReference)tr.GetObject(id, OpenMode.ForRead))
    .GroupBy(br => br.Name)
    .ToDictionary(gr => gr.Key, gr => gr.ToArray());


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 13

fsl88
Contributor
Contributor

thanks its compiling good. but when I tried it , its saying "0 found" when im window selecting on acad. 

0 Likes
Message 8 of 13

_gile
Consultant
Consultant

Show your code.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 9 of 13

fsl88
Contributor
Contributor

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System.Data;
using System.Windows;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace BLOCKCOUNT
{
public class Class1
{

[CommandMethod("BLKCNT")]
public static void Test()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;

var filter = new SelectionFilter(new[]
{
new TypedValue(0, "INSERT"),
new TypedValue(2, "name1,name2,name3")
});
var psr = ed.GetSelection(filter);
if (psr.Status != PromptStatus.OK)
return;

var ppr = ed.GetPoint("\nSpecify the table insertion point: ");
if (ppr.Status != PromptStatus.OK)
return;

using (var tr = db.TransactionManager.StartTransaction())
{

/*var blocks = psr.Value
.Cast<SelectedObject>()
.Select(so => (BlockReference)tr.GetObject(so.ObjectId, OpenMode.ForRead))
.GroupBy(br => br.Name)
.ToDictionary(gr => gr.Key, gr => gr.ToArray());*/

var blocks = psr.Value.GetObjectIds()
.Select(id => (BlockReference)tr.GetObject(id, OpenMode.ForRead))
.GroupBy(br => br.Name)
.ToDictionary(gr => gr.Key, gr => gr.ToArray());

double rowHeight = 9.0;
double colWidth = 50.0;
var table = new Table();
table.TableStyle = db.Tablestyle;
table.Cells[0, -1].Style = "_TITLE";
table.Columns[0].Width = colWidth;
table.InsertRows(1, rowHeight, blocks.Count + 1);
table.InsertColumns(1, colWidth, 2);
table.Cells[0, 0].Value = "BLOCK DATA";
table.Cells[1, 0].Value = "PREVIEW";
table.Cells[1, 1].Value = "BLOCK NAME";
table.Cells[1, 2].Value = "COUNT";
int row = 2;
foreach (var entry in blocks)
{
table.Cells[row, 0].BlockTableRecordId = entry.Value[0].BlockTableRecord;
table.Cells[row, 1].Value = entry.Key;
table.Cells[row, 2].Value = entry.Value.Length;
row++;
}
table.Position = ppr.Value;
table.TransformBy(ed.CurrentUserCoordinateSystem);
var cSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
cSpace.AppendEntity(table);
tr.AddNewlyCreatedDBObject(table, true);
tr.Commit();
}
}

}
}

0 Likes
Message 10 of 13

_gile
Consultant
Consultant

Does your drawing contains blocks named name1 or name2 or name3? because the selection filter only search for these block names.

var filter = new SelectionFilter(new[]
{
    new TypedValue(0, "INSERT"),
    new TypedValue(2, "name1,name2,name3")
});

You should not just copy/paste codes you do not understand, this is the worst way to learn.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 11 of 13

essam-salah
Collaborator
Collaborator

Hi @_gile 

what is the difference between these filters 

TypedValue(2, "name1,name2,name3")

 

 

 

 var filter = new SelectionFilter(new[] 
            {
                new TypedValue(0, "INSERT"),
                new TypedValue(-4, "<or"),
                new TypedValue(2, "name1"),
                new TypedValue(2, "name2"),
                new TypedValue(2, "name3"),
                new TypedValue(-4, "or>"),
            });

 

 

 

 

 var filter = new SelectionFilter(new[] 
            {
                new TypedValue(-4, "<and"),
                new TypedValue(0, "INSERT"),
                new TypedValue(-4, "<or"),
                new TypedValue(2, "name1"),
                new TypedValue(2, "name2"),
                new TypedValue(2, "name3"),
                new TypedValue(-4, "or>"),
                new TypedValue(-4, "and>"),

            });

 

0 Likes
Message 12 of 13

_gile
Consultant
Consultant

They work the same way, some are just most verbose.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 13 of 13

essam-salah
Collaborator
Collaborator

@_gile i was confused, thanks for clearing that up.

0 Likes