@@ -355,11 +355,12 @@ std::string getEntityName(const Dxf_EntityVariant& entityVar)
355355
356356DxfStringRef 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
365366const 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