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

Sync Branch 20240619 mme #6 support dynamic blocks read evaluation graphs and block visibility parameters #419

Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 4 additions & 69 deletions README.md
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should not be modified.

Original file line number Diff line number Diff line change
@@ -1,74 +1,9 @@
ACadSharp ![Build&Test](https://github.com/DomCr/ACadSharp/actions/workflows/build_n_test.yml/badge.svg) ![License](https://img.shields.io/github/license/DomCr/ACadSharp) ![nuget](https://img.shields.io/nuget/v/Acadsharp) [![Coverage Status](https://coveralls.io/repos/github/DomCR/ACadSharp/badge.svg?branch=master)](https://coveralls.io/github/DomCR/ACadSharp?branch=master)
---
# ACadSharp forked from https://github.com/DomCR/AcadSharp.

C# library to read/write cad files like dxf/dwg.
This fork is used to implement contributions to DomCR/AcadSharp.

### Features
The _master_ branch is kept synchronized with the _master_ in DomCR/AcadSharp.

ACadSharp allows to read or create CAD files using .Net and also extract or modify existing content in the files, the main features may be listed as:
A _merged_ branch (https://github.com/nanoLogika/ACadSharp/tree/7-merged-branch-for-acadsvg) is maintained containing all contributions being processed as needed for https://github.com/nanoLogika/ACadSvg etc.

- Read/Write Dxf binary files
- Read/Write Dxf ASCII files
- Read Dwg files
- Write Dwg files
- Extract/Modify the geometric information from the different [entities](https://help.autodesk.com/view/OARX/2021/ENU/?guid=GUID-7D07C886-FD1D-4A0C-A7AB-B4D21F18E484) in the model
- Control over the table elements like Blocks, Layers and Styles, allows you to read, create or modify the different tables

For pdf export check :construction: [ACadSharp.Pdf](https://github.com/DomCR/ACadSharp.Pdf) :construction:.

#### Compatible Dwg/Dxf versions:

| | DxfReader | DxfWriter | DwgReader | DwgWriter |
------ | :-------: | :-------: | :-------: | :-------: |
AC1009 | :x: | :x: | :x: | :x: |
AC1012 | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: |
AC1014 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
AC1015 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
AC1018 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
AC1021 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: |
AC1024 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
AC1027 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: |
AC1032 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |

Code Example
---

```c#
public static void Main()
{
string path = "sample.dwg";
CadDocument doc = DwgReader.Read(path, onNotification);
}

// Process a notification form the reader
private static void onNotification(object sender, NotificationEventArgs e)
{
Console.WriteLine(e.Message);
}
```

For more examples [check](https://github.com/DomCR/ACadSharp/tree/master/src/ACadSharp.Examples).

For more information visit the :construction: [wiki](https://github.com/DomCR/ACadSharp/wiki) :construction: .

Building
---
Before building run:
```console
git submodule update --init --recursive
```
This command will clone the submodules. These are Shared Projects that this project dependes on.

Contributing
---

Please feel free to fork this repo and send a pull request if you want to contribute to this project.

Notice that this project is in an alpha version, not all the features are implemented and there can be bugs due to this so any PR with a bug fix will not have a priority.

If you want to contribute you can check the Dxf documentation [here](https://help.autodesk.com/view/OARX/2021/ENU/?guid=GUID-235B22E0-A567-4CF6-92D3-38A2306D73F3).

License
---

This project is licensed under the MIT License - see the [LICENSE](https://github.com/DomCR/ACadSharp/blob/master/LICENSE) file for details
2 changes: 2 additions & 0 deletions src/ACadSharp/DxfFileToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public static class DxfFileToken
public const string ObjectSortEntsTable = "SORTENTSTABLE";
public const string ObjectXRecord = "XRECORD";
public const string ObjectMLeaderContextData = "CONTEXT_DATA";
public const string ObjectEvalGraph = "ACAD_EVALUATION_GRAPH";
public const string ObjectBlockVisibilityParameter = "BLOCKVISIBILITYPARAMETER";

#endregion
}
Expand Down
2 changes: 2 additions & 0 deletions src/ACadSharp/DxfSubclassMarker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@ public static class DxfSubclassMarker
public const string PlotSettings = "AcDbPlotSettings";
public const string DictionaryVariables = "DictionaryVariables";
public const string Scale = "AcDbScale";
public const string EvalGraph = "AcDbEvalGraph";
public const string BlockVisibilityParameter = "AcDbBlockVisibilityParameter";
}
}
133 changes: 132 additions & 1 deletion src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
using ACadSharp.Tables;
using ACadSharp.Tables.Collections;
using CSMath;
using CSUtilities.IO;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System;
using ACadSharp.Types;
using static ACadSharp.Objects.MultiLeaderAnnotContext;
using System.Net;
using CSUtilities.Converters;
using CSUtilities.Extensions;
using static ACadSharp.Objects.MultiLeaderAnnotContext;

namespace ACadSharp.IO.DWG
{
Expand Down Expand Up @@ -1036,6 +1039,12 @@ private CadTemplate readUnlistedType(short classNumber)
case "XRECORD":
template = this.readXRecord();
break;
case "ACAD_EVALUATION_GRAPH":
template = this.readEvaluationGraph();
break;
case "BLOCKVISIBILITYPARAMETER":
template = this.readBlockVisibilityParameter();
break;
default:
break;
}
Expand All @@ -1059,6 +1068,128 @@ private CadTemplate readUnlistedType(short classNumber)
return template;
}

#region Evaluation Graph, Enhanced Block etc.

private CadTemplate readEvaluationGraph() {
EvaluationGraph evaluationGraph = new EvaluationGraph();
EvaluationGraphTemplate template = new EvaluationGraphTemplate(evaluationGraph);

this.readCommonNonEntityData(template);

// DXF fields 96, 97 contain the value 5, here are three fields returning the same value 5
var val1 = _objectReader.ReadBitLong();
var val2 = _objectReader.ReadBitLong();
var val3 = _objectReader.ReadBitLong();
int nodeCount = val3;

for (int i = 0; i < nodeCount; i++) {
var node = new EvaluationGraph.GraphNode();
evaluationGraph.Nodes.Add(node);
node.Index = _objectReader.ReadBitLong();
node.Flags = _objectReader.ReadBitLong();
node.NextNodeIndex = _objectReader.ReadBitLong();
template.NodeHandles.Add(node, this.handleReference());
node.Data1 = _objectReader.ReadBitLong();
node.Data2 = _objectReader.ReadBitLong();
node.Data3 = _objectReader.ReadBitLong();
node.Data4 = _objectReader.ReadBitLong();
}

foreach (EvaluationGraph.GraphNode node in evaluationGraph.Nodes) {
int nextNodeIndex = node.NextNodeIndex;
if (nextNodeIndex >= 0 && nextNodeIndex < nodeCount) {
node.Next = evaluationGraph.Nodes[nextNodeIndex];
}
}

var val15 = _objectReader.ReadBitLong();

return template;
}


private CadTemplate readBlockVisibilityParameter() {
BlockVisibilityParameter blockVisibilityParameter = new BlockVisibilityParameter();
BlockVisibilityParameterTemplate template = new BlockVisibilityParameterTemplate(blockVisibilityParameter);

this.readCommonNonEntityData(template);

//analyse02(200);

var l1 = _objectReader.ReadBitLong();
var s2 = _objectReader.ReadBitShort(); // can also be L
var s3 = _objectReader.ReadBitShort(); // can also be L
var b4 = _objectReader.ReadBit();
var s5 = _objectReader.ReadBitShort(); // can also be L
var b6 = _objectReader.ReadBit();
var s7 = _objectReader.ReadBitShort(); // can also be L

var b_8 = _objectReader.ReadBit();
var b_9 = _objectReader.ReadBit();
var b_10 = _objectReader.ReadBit();
var b_11 = _objectReader.ReadBit();
var b_12 = _objectReader.ReadBit();
var b_13 = _objectReader.ReadBit();
var S_14 = _objectReader.ReadBitShort(); // can also be L

var s_15 = _objectReader.ReadBitShort();
var b_16 = _objectReader.ReadBit();
var b_17 = _objectReader.ReadBit();
var s_18 = _objectReader.ReadBitShort();

// 300 Parameter Type
blockVisibilityParameter.ParameterType = _textReader.ReadVariableText();

//resetPosition(214275, 2);
// 1010, 1020, 1030 Menu position
blockVisibilityParameter.BasePosition = _objectReader.Read3BitDouble();
// 2x0 <-
var s170 = _objectReader.ReadBitShort();
var s171 = _objectReader.ReadBitShort();
var l93 = _objectReader.ReadBitLong();
//DwgAnalyseTools.ShowCurrentPosAndShift();

//var s281 = _objectReader.ReadBitShort();

// 301
blockVisibilityParameter.Name = _textReader.ReadVariableText();
// 302
blockVisibilityParameter.Description = _textReader.ReadVariableText();
// DXF 91
blockVisibilityParameter.L91 = _objectReader.ReadBitLong();
//DwgAnalyseTools.resetPosition(214293, 0);
// DXF 93 Total entities count (no property)
var totalEntitiesCount = _objectReader.ReadBitLong();
for (int i = 0; i < totalEntitiesCount; i++) {
var handle = this.handleReference();
template.TotalEntityHandles.Add(handle, null);
}

// DXF 92 Sub blocks count (no property)
var subBlocksCount = _objectReader.ReadBitLong();
for (int sbi = 0; sbi < subBlocksCount; sbi++) {
BlockVisibilityParameter.SubBlock subBlock = new BlockVisibilityParameter.SubBlock();
subBlock.Name = _textReader.ReadVariableText();
blockVisibilityParameter.SubBlocks.Add(subBlock);

IList<ulong> subBlockHandles = new List<ulong>();
template.SubBlockHandles.Add(subBlock, subBlockHandles);
// DXF 94 Subblock entities count (no property)
int entitiesCount = _objectReader.ReadBitLong();
for (int i = 0; i < entitiesCount; i++) {
var handle = this.handleReference();
subBlockHandles.Add(handle);
}
//DwgAnalyseTools.showCurrentPosAndShift();
// DXF 95
var endMark = _objectReader.ReadBitLong();
}

return template;
}

#endregion

#region Text entities

private CadTemplate readUnknownEntity(DxfClass dxfClass)
Expand Down
42 changes: 42 additions & 0 deletions src/ACadSharp/IO/Templates/BlockVisibilityParameterTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;

using ACadSharp.Entities;
using ACadSharp.Objects;

namespace ACadSharp.IO.Templates {

internal class BlockVisibilityParameterTemplate : CadTemplate<BlockVisibilityParameter> {

public BlockVisibilityParameterTemplate(BlockVisibilityParameter cadObject)
: base(cadObject) {
}

public IDictionary<ulong, Entity> TotalEntityHandles { get; } = new Dictionary<ulong, Entity>();

public IDictionary<BlockVisibilityParameter.SubBlock, IList<ulong>> SubBlockHandles { get; } = new Dictionary<BlockVisibilityParameter.SubBlock, IList<ulong>>();

public override void Build(CadDocumentBuilder builder) {
base.Build(builder);

foreach (var cadObjectHandle in this.TotalEntityHandles) {
ulong handle = cadObjectHandle.Key;
if (builder.TryGetCadObject(handle, out Entity entity)) {
this.TotalEntityHandles[handle] = entity;
this.CadObject.Entities.Add(entity);
}
}

foreach (var subGroup in this.CadObject.SubBlocks) {
if (this.SubBlockHandles.TryGetValue(subGroup, out IList<ulong> subBlockHandles)) {
foreach (ulong handle in subBlockHandles) {
if (this.TotalEntityHandles.TryGetValue(handle, out Entity entity)) {
subGroup.Entities.Add(entity);
}
else if (builder.TryGetCadObject(handle, out Entity entityX)) {
}
}
}
}
}
}
}
25 changes: 25 additions & 0 deletions src/ACadSharp/IO/Templates/EvaluationGraphTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;

using ACadSharp.Objects;

namespace ACadSharp.IO.Templates {
internal class EvaluationGraphTemplate : CadTemplate<EvaluationGraph>{

public EvaluationGraphTemplate(EvaluationGraph evaluationGraph)
: base(evaluationGraph) {
}

public IDictionary<EvaluationGraph.GraphNode, ulong> NodeHandles { get; } = new Dictionary<EvaluationGraph.GraphNode, ulong>();

public override void Build(CadDocumentBuilder builder) {
base.Build(builder);

foreach (EvaluationGraph.GraphNode node in this.CadObject.Nodes) {
var nodeHandle = this.NodeHandles[node];
if (builder.TryGetCadObject(nodeHandle, out CadObject nodeObject)) {
node.NodeObject = nodeObject;
}
}
}
}
}
Loading
Loading