Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Attributes #304

Open
Gora1000 opened this issue Mar 7, 2024 · 3 comments
Open

Get Attributes #304

Gora1000 opened this issue Mar 7, 2024 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@Gora1000
Copy link

Gora1000 commented Mar 7, 2024

Hello

how is the best way to get the attributes from a Block in a Layout?

@DomCR DomCR added the help wanted Extra attention is needed label Mar 12, 2024
@DomCR
Copy link
Owner

DomCR commented Mar 12, 2024

Hi @Gora1000

To get the attributes for an instance of a block you have to get all the Inserts in the model, like:

CadDocument doc = DwgReader.Read(path);
foreach (Insert insert in doc.Entities.OfType<Insert>())
{
	//Do something with: insert.Attributes;
}

Let me know if this helps.

@Gora1000
Copy link
Author

Hi DomCr,

thaks for your answer. With doc.Entities you get all blocks of type insert in the model space, but not from layout. For exsample I added a test.dwg to my answer.
[Test.zip]

I you open the file, you will find 3 Blocks in the Model space and in each layout the same one. In test.dwg two Layouts. If you use
CadObjectCollection<Entity> ent= doc.Entities;

you will get only three blocks from model space, but i need the Attributes from the Block in Layout.

If it should work, and i can change attribute values on the block in a layout. It is possible to save the changes at the same dwg file?

@DomCR
Copy link
Owner

DomCR commented Mar 14, 2024

Each Layout has it's own Block associated to it, which is the one that contains all the entities, you can iterate throw all layouts using:

foreach (Objects.Layout layout in doc.Layouts)
{
      BlockRecord block = layout.AssociatedBlock;
      foreach (Insert insert in block.Entities.OfType<Insert>())
      {
	      //Do something with: insert.Attributes
      }
}

This way you will get all the block instances (Insert) in the Layout that you want.

You can save the file using DwgWriter but keep in mind that ACadSharp doesn't guarantees the integrity of the resulting file, so I would not recommend to override the file, instead save it with a different name.

Let me know if this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants