Author Topic: Access XData in .dfx  (Read 428 times)

Offline billo

  • Ewok
  • *
  • Posts: 23
    • View Profile
Access XData in .dfx
« on: January 17, 2023, 22:36:14 pm »
I've exported a .dfx from Rhino that contains one XData field that I would like to access from a script (along with LayerName). In browsing the Entity object in an Object Browser I don't see anything that looks promising.

I'm guessing this is not possible. If someone knows how to do this please share.

(My best workaround might be to encode data into the Layer property in Rhino and then decode in CamBam, but that gets ugly in a hurry.)

Bill

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7146
    • View Profile
    • Cambam V1.0 French Doc
Re: Access XData in .dfx
« Reply #1 on: January 17, 2023, 23:55:40 pm »
Hello

I think that XData are not used by Cambam after the DXF has been converted, certainly your script must explore the DXF file itself to extract what you want, not the Entities created by Cambam when importing.

++
David

Offline billo

  • Ewok
  • *
  • Posts: 23
    • View Profile
Re: Access XData in .dfx
« Reply #2 on: January 21, 2023, 18:46:56 pm »
My original goal in using XData was to implement a "sublayer" for each Entity to go along with the normal "Layer" in my Rhino export.

It turned out to be much easier than I thought to get these two items of metadata into CamBam.

I belatedly discovered that Rhino, in fact, does provide sublayers. In exporting to .dfx, Rhino encodes these layers as "layer$sublayer".

A simple python script decodes this .dfx "layer" into Entity Layer and Tag properties.

Code: [Select]
# Process Rhino sublayers. Decode "layer$sublayer" to Entity Layer and Tag

from CamBam.CAM import *
from CamBam.UI import CamBamUI
from CamBam.Values import *

doc = CamBamUI.MainUI.ActiveView.CADFile
view = CamBamUI.MainUI.ActiveView
layerColor = DXF.DXFColor().GetColor(0) # black

def decodeLayersToTags():
    view.SelectAllVisibleGeometry()
    allEntities = view.SelectedEntities

    for (entity) in allEntities:
        layerName = entity.Layer.Name
       
        # See if this object in on a sublayer. Rhino encodes sublayers as "layer$sublayer" (the sublayer is the slat (part) number
        if ("$" in layerName):
            layerTagPair = layerName.split("$")
            layerName = layerTagPair[0]
            entity.Tag = layerTagPair[1] # store the part number in the Entity Tag

            if (not doc.HasLayer(layerName)): # create the base layer if it doesn't exist yet
                newLayer = doc.CreateLayer(layerName)
                newLayer.Color = layerColor

            # remove the Entity and add it back with the new (Active)Layer
            doc.RemovePrimitive(entity)
            doc.SetActiveLayer(layerName)
            doc.Add(entity)
   
decodeLayersToTags()
for (layer) in doc.Layers.Clone():
    if ("$" in layer.Name):
        doc.Layers.Remove(layer)
doc.SetActiveLayer("0")

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7146
    • View Profile
    • Cambam V1.0 French Doc
Re: Access XData in .dfx
« Reply #3 on: January 21, 2023, 20:00:34 pm »
Nice job ;)

++
David

Offline billo

  • Ewok
  • *
  • Posts: 23
    • View Profile
Re: Access XData in .dfx
« Reply #4 on: January 21, 2023, 22:15:32 pm »
One remaining nit: after running the script, all objects appear on their proper layer in the Layer treeview, but the property panel shows "Default" instead of the layer indicated by the Layer treeview.

I tried view.RefreshView(), but that had no effect. Is there another kind of refresh required to make the property panel show the updated value for layer?

Everything works fine, so this is simply a minor display problem I can live with.

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7146
    • View Profile
    • Cambam V1.0 French Doc
Re: Access XData in .dfx
« Reply #5 on: January 22, 2023, 00:18:58 am »

Offline billo

  • Ewok
  • *
  • Posts: 23
    • View Profile
Re: Access XData in .dfx
« Reply #6 on: January 22, 2023, 01:30:55 am »
That looked encouraging, but it didn't work.

Of course, if I save the .cb file and open it again, all the layers show correctly.

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7146
    • View Profile
    • Cambam V1.0 French Doc
Re: Access XData in .dfx
« Reply #7 on: January 22, 2023, 16:13:26 pm »
there is also a

CamBam.UI.CBFileTreeView.ReloadTree()

++
David

Offline billo

  • Ewok
  • *
  • Posts: 23
    • View Profile
Re: Access XData in .dfx
« Reply #8 on: January 23, 2023, 01:18:05 am »
I embedded the code into a larger script. I can't see any obvious change in environment, but now the Layer is showing properly in the Property panel. I'll take the win. (I did not need to use any version of ReloadTree).

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7146
    • View Profile
    • Cambam V1.0 French Doc
Re: Access XData in .dfx
« Reply #9 on: January 23, 2023, 01:48:11 am »
 ;D

Cool

++
David