Skip to content

Commit 501c729

Browse files
committed
IO_DXF: WIP3
1 parent 45316aa commit 501c729

File tree

4 files changed

+82
-42
lines changed

4 files changed

+82
-42
lines changed

src/io_dxf/dxf_format_entities.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct Dxf_BaseEntity {
2929
// Code 67
3030
enum class Space { Model = 0, Paper = 1 };
3131
Space space = Space::Model;
32+
// TODO Code 420: "true" RGB color(24bit)
3233
};
3334

3435
struct Dxf_BaseGeom2dEntity : public Dxf_BaseEntity {

src/io_dxf/dxf_parser.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ const Dxf_STYLE* DxfParser::findStyle(DxfStringRef name) const
222222
return it != m_mapStyle.cend() ? it->second : nullptr;
223223
}
224224

225+
void DxfParser::addLayer(Dxf_LAYER&& layer)
226+
{
227+
if (this->findLayer(layer.name) == nullptr) {
228+
m_layers.push_back(std::forward<Dxf_LAYER>(layer));
229+
const auto& layerStored = m_layers.back();
230+
m_mapLayer.insert({layerStored.name, &layerStored});
231+
}
232+
}
233+
225234
bool DxfParser::parseEntity(
226235
const std::function<void()>& fnEntityHandler,
227236
const std::function<void(int)>& fnCodeHandler,
@@ -988,6 +997,7 @@ bool DxfParser::parseInsert()
988997

989998
bool DxfParser::parseDimension()
990999
{
1000+
#if 0
9911001
DxfCoords s = {}; // startpoint
9921002
DxfCoords e = {}; // endpoint
9931003
DxfCoords p = {}; // dimpoint
@@ -1035,6 +1045,7 @@ bool DxfParser::parseDimension()
10351045
break;
10361046
}
10371047
}
1048+
#endif
10381049

10391050
return false;
10401051
}
@@ -1170,12 +1181,11 @@ bool DxfParser::parseLayer()
11701181
const int n = stringToInt(m_str, StringToErrorMode::ReturnErrorValue);
11711182
if (n == 0) {
11721183
if (layer.name.empty()) {
1173-
this->reportError_readInteger("DXF::parseLayer() - no layer name");
1184+
this->reportError("DXF::parseLayer() - no layer name");
11741185
return false;
11751186
}
11761187

1177-
m_layers.push_back(std::move(layer));
1178-
m_mapLayer.insert({m_layers.back().name, &m_layers.back()});
1188+
this->addLayer(std::move(layer));
11791189
return true;
11801190
}
11811191
else if (isStringToErrorValue(n)) {
@@ -1481,22 +1491,26 @@ void DxfParser::parse(std::istream& stream)
14811491
continue;
14821492
}
14831493
else {
1484-
m_fail = false;
1494+
m_fail = true;
14851495
std::string errMsg = "DXF::parse() - Failed to parse " + m_str;
14861496
if (!exceptionMsg.empty())
14871497
errMsg += "\nError: " + exceptionMsg;
14881498

14891499
this->reportError(errMsg);
1490-
if (m_str == "LAYER") // Some objects or tables can have "LAYER" as name...
1491-
continue;
1492-
else
1493-
return;
14941500
}
14951501
}
14961502
}
14971503

14981504
getLine();
14991505
}
1506+
1507+
// Ensure layer "0" is there
1508+
{
1509+
Dxf_LAYER layer0;
1510+
layer0.name = "0";
1511+
layer0.colorId = 7;
1512+
this->addLayer(std::move(layer0));
1513+
}
15001514
}
15011515

15021516
void DxfParser::setGetLinePostCallback(std::function<void(size_t)> fn)

src/io_dxf/dxf_parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class DxfParser {
115115
std::string_view entityTypeName
116116
);
117117

118+
void addLayer(Dxf_LAYER&& layer);
119+
118120
template<typename EntityValue, typename Entity>
119121
void addEntity(Entity&& entity, std::deque<EntityValue>& entityStore);
120122

src/io_dxf/io_dxf.cpp

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,12 @@ std::string getEntityName(const Dxf_EntityVariant& entityVar)
355355

356356
DxfStringRef getEntityLayerName(const Dxf_EntityVariant& entityVar)
357357
{
358-
return std::visit(Cpp::Overloaded{
358+
DxfStringRef layerName = std::visit(Cpp::Overloaded{
359359
[](std::monostate) { return DxfStringRef{}; },
360360
[](auto cref) { return cref.get().layerName; }
361361
}, entityVar
362362
);
363+
return !layerName.empty() ? layerName : "0";
363364
}
364365

365366
const Dxf_BaseEntity* getBaseEntityPtr(const Dxf_EntityVariant& entityVar)
@@ -495,7 +496,6 @@ TDF_LabelSequence DxfReader::transfer(DocumentPtr doc, TaskProgress* progress)
495496
OccHandle<XCAFDoc_ColorTool> colorTool = doc->xcaf().colorTool();
496497
OccHandle<XCAFDoc_LayerTool> layerTool = doc->xcaf().layerTool();
497498
std::unordered_map<DxfStringRef, TDF_Label> mapLayerNameLabel;
498-
std::unordered_map<DxfColorIndex, TDF_Label> mapAciColorLabel;
499499

500500
auto fnAddRootShape = [&](const TopoDS_Shape& shape, std::string_view shapeName, TDF_Label layer) {
501501
const TDF_Label labelShape = shapeTool->NewShape();
@@ -508,29 +508,47 @@ TDF_LabelSequence DxfReader::transfer(DocumentPtr doc, TaskProgress* progress)
508508
return labelShape;
509509
};
510510

511-
auto fnAddAci = [&](DxfColorIndex aci) -> TDF_Label {
512-
auto it = mapAciColorLabel.find(aci);
513-
if (it != mapAciColorLabel.cend())
511+
std::unordered_map<DxfColorIndex, TDF_Label> mapColorIndexLabel;
512+
auto fnAddColorIndex = [&](DxfColorIndex aci) -> TDF_Label {
513+
auto it = mapColorIndexLabel.find(aci);
514+
if (it != mapColorIndexLabel.cend())
514515
return it->second;
515516

516517
if (0 <= aci && Cpp::cmpLess(aci, std::size(aciTable))) {
517518
const RGB_Color& c = aciTable[aci].second;
518519
const TDF_Label colorLabel = colorTool->AddColor(
519520
Quantity_Color(c.r / 255., c.g / 255., c.b / 255., Quantity_TOC_RGB)
520521
);
521-
mapAciColorLabel.insert({ aci, colorLabel });
522+
mapColorIndexLabel.insert({ aci, colorLabel });
522523
return colorLabel;
523524
}
524525

525526
return TDF_Label{};
526527
};
527528

528-
auto fnSetShapeColor = [=](const TDF_Label& labelShape, DxfColorIndex aci) {
529-
const TDF_Label labelColor = fnAddAci(aci);
529+
auto fnSetShapeColor = [=](TDF_Label labelShape, DxfColorIndex aci) {
530+
const TDF_Label labelColor = fnAddColorIndex(aci);
530531
if (!labelColor.IsNull())
531532
colorTool->SetColor(labelShape, labelColor, XCAFDoc_ColorGen);
532533
};
533534

535+
auto fnExplicitColorIndex = [](const Dxf_EntityVariant& entityVar, const Dxf_LAYER* dxfLayer) {
536+
auto baseEntityPtr = getBaseEntityPtr(entityVar);
537+
if (baseEntityPtr) {
538+
if (baseEntityPtr->colorId == dxfColorByLayer && dxfLayer != nullptr) {
539+
return dxfLayer->colorId;
540+
}
541+
else if (baseEntityPtr->colorId == dxfColorByBlock) {
542+
// TODO
543+
return baseEntityPtr->colorId;
544+
}
545+
546+
return baseEntityPtr->colorId;
547+
}
548+
549+
return -1;
550+
};
551+
534552
if (m_params.groupLayers) {
535553
using ArrayOfTransferObjects = std::vector<TransferObject>;
536554
std::unordered_map<const Dxf_LAYER*, ArrayOfTransferObjects> mapObjectsByLayer;
@@ -539,35 +557,32 @@ TDF_LabelSequence DxfReader::transfer(DocumentPtr doc, TaskProgress* progress)
539557
if (entityShape.IsNull())
540558
continue; // Skip
541559

542-
DxfStringRef layerName = getEntityLayerName(entityVar);
543-
const Dxf_LAYER* layer = m_impl->findLayer(layerName);
544-
auto it = mapObjectsByLayer.find(layer);
545-
if (it == mapObjectsByLayer.cend())
546-
it = mapObjectsByLayer.insert({layer, ArrayOfTransferObjects{}}).first;
560+
const Dxf_LAYER* dxfLayer = m_impl->findLayer(getEntityLayerName(entityVar));
561+
if (!dxfLayer)
562+
dxfLayer = m_impl->findLayer("0");
547563

548-
assert(it != mapObjectsByLayer.cend());
549-
TransferObject object;
550-
object.entityVariantPtr = &entityVar;
551-
object.shape = entityShape;
552-
// Resolve color
553-
auto baseEntityPtr = getBaseEntityPtr(entityVar);
554-
if (baseEntityPtr) {
555-
if (baseEntityPtr->colorId == dxfColorByLayer) {
556-
object.explicitColorId = layer->colorId;
557-
}
558-
else if (baseEntityPtr->colorId == dxfColorByBlock) {
559-
// TODO
560-
}
561-
else {
562-
object.explicitColorId = baseEntityPtr->colorId;
564+
assert(dxfLayer != nullptr);
565+
ArrayOfTransferObjects* arrayOfTransferObjects = nullptr;
566+
{
567+
auto it = mapObjectsByLayer.find(dxfLayer);
568+
if (it == mapObjectsByLayer.cend()) {
569+
it = mapObjectsByLayer.insert({dxfLayer, ArrayOfTransferObjects{}}).first;
570+
assert(it != mapObjectsByLayer.cend());
563571
}
572+
573+
arrayOfTransferObjects = &it->second;
564574
}
565575

566-
it->second.push_back(std::move(object));
576+
TransferObject object;
577+
object.entityVariantPtr = &entityVar;
578+
object.shape = entityShape;
579+
object.explicitColorId = fnExplicitColorIndex(entityVar, dxfLayer);
580+
assert(arrayOfTransferObjects != nullptr);
581+
arrayOfTransferObjects->push_back(std::move(object));
567582
}
568583

569584
for (const auto& [layer, objects] : mapObjectsByLayer) {
570-
TopoDS_Compound layerShape = BRepUtils::makeEmptyCompound();
585+
TopoDS_Shape layerShape;
571586
for (const TransferObject& obj : objects) {
572587
if (!obj.shape.IsNull())
573588
BRepUtils::addShape(&layerShape, obj.shape);
@@ -580,21 +595,29 @@ TDF_LabelSequence DxfReader::transfer(DocumentPtr doc, TaskProgress* progress)
580595
const TDF_Label shapeLabel = fnAddRootShape(layerShape, layer->name, {});
581596
// Check if all entities have the same color
582597
bool uniqueColor = true;
583-
const DxfColorIndex aci = !objects.empty() ? objects.front().explicitColorId : -1;
598+
assert(!objects.empty());
599+
const DxfColorIndex aci = objects.front().explicitColorId;
584600
for (const TransferObject& obj : objects) {
585601
uniqueColor = obj.explicitColorId == aci;
586602
if (!uniqueColor)
587603
break;
604+
605+
if (dxfEntityVariantGet<Dxf_INSERT>(*obj.entityVariantPtr) != nullptr) {
606+
auto dxfInsert = dxfEntityVariantGet<Dxf_INSERT>(*obj.entityVariantPtr);
607+
const Dxf_BLOCK* block = m_impl->findBlock(dxfInsert->blockName);
608+
if (block) {
609+
for (const Dxf_EntityVariant& subEntity : block->entities) {
610+
}
611+
}
612+
}
588613
}
589614

590615
if (uniqueColor) {
591616
fnSetShapeColor(shapeLabel, aci);
592617
}
593618
else {
594619
for (const TransferObject& obj : objects) {
595-
if (obj.shape.IsNull())
596-
continue;
597-
620+
assert(!obj.shape.IsNull());
598621
const TDF_Label objShapeLabel = shapeTool->AddSubShape(shapeLabel, obj.shape);
599622
fnSetShapeColor(objShapeLabel, obj.explicitColorId);
600623
}

0 commit comments

Comments
 (0)