Having trouble with Layout API methods:

Do not appear to work?

So I’m trying the equivalent Ruby method:

Also appears to not work?
Not sure what I might be missing or bugs within the methods?

Please see the attached ruby code I’m running and a simple layout file with 1 page, couple of objects, and some non-shared layers
move_to_layer test code.txt (224 Bytes)
Untitled.layout (16.8 KB)

1 Like

Please be more specific. What is your expectation. What is the actual result?

Please be more specific. What is your expectation. What is the actual result?

Looking at the Ruby documentation, this method appears to have complex behavior based upon the scenario.

Yes, it is complex for sure! The objects on the page are on a non-shared layer ‘Layer 1’. I’m grabbing the first layer from doc.layers which should be layer ‘Default’ (based on what I was getting for index’s that should be the first one). So I’m expecting the objects to end up on ‘Default’ layer. Code execution runs fine with no errors. I open the layout file to inspect the changes but the objects remain on layer ‘Layer 1’.

I was hoping the Ruby API method would clue me into something I might be missing with the Layout API method calls.

Well, I would say confirm that with:

doc.layers.each_with_index { |layer, index|
  puts "Layer (#{index}) '#{}' is #{layer.shared? ? 'shared' : 'non-shared'}."

You may have missed the fact that the Layout::Layers class mixes in the Ruby core Enumerable library module giving it many more methods.

So rather than relying upon assumed indexes, you can get a layer from the collection thus:

layer_1 = doc.layers.find { |layer| == 'Layer 1' }
if layer_1 # nil if not found
  # move entity objects to layer_1

Before creating the topic I did run through the code and verify ‘Default’ layer is the first layer.
The objects are currently on layer ‘Layer 1’. Using Entity.move_to_layer does not result in changing the layer of the objects to layer ‘Default’.

I believe you. I tried it also. I see the same.

I also tried going through the LayerInstance.entities

layer_1 = layers.find { |layer| == 'Layer 1' }
layer_1_inst = layer_1.layer_instance(doc.pages.first)
layer_1_inst.entities.to_a.each { |entity|

And note that it does not matter if we iterate the entities directly or an array copy.

Also, I tried removing the entity and then adding it again …

page1 = doc.pages.first
layer_1_inst.entities.to_a.each { |entity|
  obj = doc.remove_entity(entity)
  doc.add_entity(obj, layer_0, page1)
#<TypeError: wrong argument type>
<main>:3:in `add_entity'

It turns out that Layout::Document#remove_entity returns nil not the removed entity.

I would say, go ahead and open an issue in the API bug tracker.

Got it Dan, I appreciate the help! I will add it to the bug tracker list.

Thank you sir!

1 Like

Hi @icadubuild,

I looked into this quickly. It does look like we have an issue with both LOEntityMoveToLayer and LOEntityListMoveToLayer when attempting to move an entity from a non-shared layer to another layer.

I verified this by running the following code, which makes the layer in your sample file that contains the entities shared first (and leaves the entities on the page):

layers = doc.layers
layers[1].set_shared(doc.pages.first, Layout::Layer::SHARELAYERACTION_KEEPONEPAGE)
entities = doc.shared_entities
entities.each { |entity|
  entity.move_to_layer(layers.first, [doc.pages.first])

Note that I also then need to grab the entities from the document’s shared_entities list to iterate.

Thanks for bringing this to our attention,

1 Like