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

Feat(dui3): CNX-203 skp create tag folders for project model and document names #374

Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 7 additions & 5 deletions speckle_connector_3/src/convertors/to_native_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,12 @@ def receive_commit_object(obj)
create_definition_proxies
create_render_materials

unless from_revit
# Create layers and it's folders from layers relation on the model collection.
SpeckleObjects::Relations::Layers.to_native(obj, obj["colorProxies"], sketchup_model, source_app, model_card.project_id, model_card.model_id)
end
#unless from_revit
# # Create layers and it's folders from layers relation on the model collection.
# SpeckleObjects::Relations::Layers.to_native(obj, obj["colorProxies"], sketchup_model, source_app, model_card)
#end

SpeckleObjects::Relations::Layers.to_native(obj, obj["colorProxies"], sketchup_model, source_app, model_card)

# By default entities to fill is sketchup model's entities.
@entities_to_fill = sketchup_model.entities
Expand Down Expand Up @@ -415,7 +417,7 @@ def convert_to_native(state, obj, layer, entities = sketchup_model.entities)
# Create levels as section planes if they exists
create_levels(state, obj)
# Create layers from category of object and place object in it
create_layers_from_categories(state, obj, converted_entities)
# create_layers_from_categories(state, obj, converted_entities)
end
# Create speckle entities from sketchup entities to achieve continuous traversal.

Expand Down
39 changes: 39 additions & 0 deletions speckle_connector_3/src/sketchup_model/query/layer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def path(layer)
parent_folders.reverse
end

# @param folder [Sketchup::LayerFolder] folder to get folder its all subfolders as flat list
def flat_folders(folder, flat_folders = [])
flat_folders.append(folder)
folder.folders.each { |sub| flat_folders(sub, flat_folders) }
flat_folders
end

# @param entity [Sketchup::Entity] entity to find path.
def entity_path(entity, separation = '::')
path = path(entity.layer)
Expand All @@ -37,6 +44,38 @@ def entity_layer_from_path(string_layer_path, separation = '::')

string_layer_path.split(separation).last
end

# @param sketchup_model [Sketchup::Model] active model
# @param layer_name [String] layer name to get the next one if exists.
def get_increment_layer_name(sketchup_model, layer_name)
return layer_name unless sketchup_model.layers.any? { |l| l.display_name == layer_name }

counter = 1
new_layer_name = layer_name
while true
new_layer_name = "#{layer_name} (#{counter})"
layer = sketchup_model.layers.find { |l| l.display_name == new_layer_name }
break if layer.nil?
counter += 1
end
new_layer_name
end

# @param sketchup_model [Sketchup::Model] active model
# @param layer_name [String] layer name to get the next one if exists.
def get_last_increment_layer(sketchup_model, layer_name)
counter = 1
previous_layer_name = layer_name
next_layer_name = layer_name
while true
layer = sketchup_model.layers.find { |l| l.display_name == next_layer_name }
break if layer.nil?
next_layer_name = "#{layer_name} (#{counter})"
previous_layer_name = counter - 1 == 0 ? layer_name : "#{layer_name} (#{counter - 1})"
counter += 1
end
previous_layer_name
end
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions speckle_connector_3/src/speckle_objects/geometry/line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ def self.to_native(state, line, layer, entities, &_convert_to_native)
UiData::Report::ConversionStatus::WARNING)
end

line_layer_name = SketchupModel::Query::Layer.entity_layer_from_path(line['layer'])
line_layer = state.sketchup_state.sketchup_model.layers.to_a.find { |l| l.display_name == line_layer_name }
# line_layer_name = SketchupModel::Query::Layer.entity_layer_from_path(line['layer'])
# line_layer = state.sketchup_state.sketchup_model.layers.to_a.find { |l| l.display_name == line_layer_name }
edges.each do |edge|
edge.layer = line_layer.nil? ? layer : line_layer
edge.layer = layer
# edge.layer = line_layer.nil? ? layer : line_layer
unless line['sketchup_attributes'].nil?
SketchupModel::Dictionary::BaseDictionaryHandler
.attribute_dictionaries_to_native(edge, line['sketchup_attributes']['dictionaries'])
Expand Down
7 changes: 4 additions & 3 deletions speckle_connector_3/src/speckle_objects/geometry/mesh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,15 @@ def self.to_native(state, mesh, layer, entities, &convert_to_native)
# Add faces from mesh with material and smooth setting
entities.add_faces_from_mesh(native_mesh, smooth_flags, material, material)
added_faces = entities.grep(Sketchup::Face).last(native_mesh.polygons.length)
mesh_layer_name = SketchupModel::Query::Layer.entity_layer_from_path(mesh['layer'])
mesh_layer = state.sketchup_state.sketchup_model.layers.to_a.find { |l| l.display_name == mesh_layer_name }
#mesh_layer_name = SketchupModel::Query::Layer.entity_layer_from_path(mesh['layer'])
#mesh_layer = state.sketchup_state.sketchup_model.layers.to_a.find { |l| l.display_name == mesh_layer_name }
# Merge only added faces in this scope
# if model_preferences[:merge_coplanar_faces]
# added_faces = Converters::CleanUp.merge_coplanar_faces(added_faces)
# end
added_faces.each do |face|
face.layer = mesh_layer unless mesh_layer.nil?
face.layer = layer
# face.layer = mesh_layer unless mesh_layer.nil?
# Smooth edges if they already soft
# FIXME: Below line should be reconsidered. It might be a good to know here mesh comes from NURBS or not.
face.edges.each { |edge| edge.smooth = true if edge.soft? } if has_any_non_planar_quad_mesh
Expand Down
18 changes: 10 additions & 8 deletions speckle_connector_3/src/speckle_objects/relations/layer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class Layer < Base

# rubocop:disable Metrics/ParameterLists
def initialize(name:, visible:, is_folder:, full_path: nil, line_style: nil, color: nil, layers_and_folders: [],
application_id: nil)
application_id: nil, id: nil)
super(
speckle_type: SPECKLE_TYPE,
application_id: application_id,
id: nil
id: id
)
self[:name] = name
self[:color] = color
Expand All @@ -34,7 +34,8 @@ def initialize(name:, visible:, is_folder:, full_path: nil, line_style: nil, col
# @param folder [Sketchup::Layers, Sketchup::LayerFolder] folder to create layers in it.
# @param sketchup_model [Sketchup::Model] sketchup active model.
def self.to_native_layer(speckle_layer, color_proxies, folder, sketchup_model, project_id, model_id)
layer = sketchup_model.layers.add_layer(speckle_layer[:name])
layer_name = SketchupModel::Query::Layer.get_increment_layer_name(sketchup_model, speckle_layer[:name])
layer = sketchup_model.layers.add_layer(layer_name)
layer.visible = speckle_layer[:visible] unless speckle_layer[:visible].nil?
if color_proxies
color_proxy = color_proxies.find { |proxy| proxy["objects"].include?(speckle_layer.application_id) }
Expand All @@ -49,6 +50,7 @@ def self.to_native_layer(speckle_layer, color_proxies, folder, sketchup_model, p
# NOTE: Deep clean purpose!
BASE_DICT.set_hash(
layer, {
speckle_id: speckle_layer[:id],
project_id: project_id,
model_id: model_id
}
Expand Down Expand Up @@ -86,18 +88,18 @@ def self.deep_clean(sketchup_model, project_id, model_id)
end

# Flat layer conversion.
def self.to_native_flat_layers(layers_relation, color_proxies, sketchup_model, project_id, model_id)
def self.to_native_flat_layers(layers_relation, color_proxies, folder, sketchup_model, project_id, model_id)
speckle_layers = layers_relation[:elements]

elements_to_layers(speckle_layers, color_proxies, sketchup_model, project_id, model_id)
elements_to_layers(speckle_layers, color_proxies, folder, sketchup_model, project_id, model_id)
end

# Converts elements to layers with it's full path.
def self.elements_to_layers(elements, color_proxies, sketchup_model, project_id, model_id)
def self.elements_to_layers(elements, color_proxies, folder, sketchup_model, project_id, model_id)
elements.each do |element|
element[:name] = element[:full_path]
to_native_layer(element, color_proxies, sketchup_model.layers, sketchup_model, project_id, model_id)
elements_to_layers(element[:elements], color_proxies, sketchup_model, project_id, model_id) unless element[:elements].nil?
to_native_layer(element, color_proxies, folder, sketchup_model, project_id, model_id)
elements_to_layers(element[:elements], color_proxies, folder, sketchup_model, project_id, model_id) unless element[:elements].nil?
end
end

Expand Down
25 changes: 18 additions & 7 deletions speckle_connector_3/src/speckle_objects/relations/layers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def self.extract_relations(commit_obj, source_app)
# rubocop:disable Metrics/CyclomaticComplexity
def self.element_to_relation(elements, source_app, parent_layers)
elements.collect do |element|
next unless element['speckle_type'] == SPECKLE_CORE_MODELS_LAYER_COLLECTION
next unless element['speckle_type'] == SPECKLE_CORE_MODELS_LAYER_COLLECTION || element['speckle_type'] == SPECKLE_CORE_MODELS_COLLECTION

layers_tree = parent_layers.dup.append(element['name'])
full_path = ''
Expand All @@ -59,29 +59,40 @@ def self.element_to_relation(elements, source_app, parent_layers)
color: nil,
full_path: full_path,
layers_and_folders: element_to_relation(element['@elements'] || element['elements'], source_app, layers_tree),
application_id: element['applicationId'],
application_id: element['applicationId'], id: element['id']
)
end.compact
end
# rubocop:enable Metrics/CyclomaticComplexity

def self.to_native(obj, color_proxies, sketchup_model, source_app, project_id, model_id)
# @param sketchup_model [Sketchup::Model]
# @param model_card [Cards::Card] model card
def self.to_native(obj, color_proxies, sketchup_model, source_app, model_card)
layers_relation = extract_relations(obj, source_app)
return if layers_relation.nil?
model_id = model_card.model_id
project_id = model_card.project_id

folder_name = "#{model_card.project_name}-#{model_card.model_name}"
existing_folder = sketchup_model.layers.folders.find { |f| f.display_name == folder_name }
if existing_folder # folders cleanup!
subfolders = SketchupModel::Query::Layer.flat_folders(existing_folder)
subfolders.each { |f| sketchup_model.layers.remove_folder(f) }
end
model_folder = sketchup_model.layers.add_folder("#{model_card.project_name}-#{model_card.model_name}")

folder = sketchup_model.layers
is_flat = source_app.include?('rhino') # flat by meaning -> adds :: for children

# FIXME: UPDATE BEHAVIOR: !!! NOT SURE it is a good idea !!!
SpeckleObjects::Relations::Layer.deep_clean(sketchup_model, project_id, model_id)

if is_flat
SpeckleObjects::Relations::Layer.to_native_flat_layers(layers_relation, color_proxies, sketchup_model, project_id, model_id)
SpeckleObjects::Relations::Layer.to_native_flat_layers(layers_relation, color_proxies, model_folder, sketchup_model, project_id, model_id)
else
SpeckleObjects::Relations::Layer.to_native_layer_folder(layers_relation, color_proxies, folder, sketchup_model, project_id, model_id)
SpeckleObjects::Relations::Layer.to_native_layer_folder(layers_relation, color_proxies, model_folder, sketchup_model, project_id, model_id)
end

active_layer = folder.to_a.find { |layer| layer.display_name == layers_relation['active_layer'] }
active_layer = sketchup_model.layers.to_a.find { |layer| layer.display_name == layers_relation['active_layer'] }
sketchup_model.active_layer = active_layer unless active_layer.nil?
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ def initialize(name:, collection_type:, elements: [], application_id: nil)
end

def self.to_native(state, collection, layer, entities, &convert_to_native)
collection_type = collection['collectionType']

if collection_type && collection_type.include?('layer')
return LayerCollection.to_native(state, collection, layer, entities, &convert_to_native)
end

ModelCollection.to_native(state, collection, layer, entities, &convert_to_native)
# Always iterate as layer collection with V3
LayerCollection.to_native(state, collection, layer, entities, &convert_to_native)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Models
# LayerCollection object that collect other speckle objects under it's elements.
class LayerCollection < Collection
SPECKLE_TYPE = SPECKLE_CORE_MODELS_LAYER_COLLECTION
BASE_DICT = SketchupModel::Dictionary::SpeckleEntityDictionaryHandler
# rubocop:disable Metrics/ParameterLists
def initialize(name:, visible:, is_folder:, display_style: nil, color: nil, elements: [],
application_id: nil)
Expand Down Expand Up @@ -101,10 +102,16 @@ def self.get_or_create_layer_collection(entity_layer, collection)
def self.to_native(state, layer_collection, layer_or_folder, entities, &convert_to_native)
sketchup_model = state.sketchup_state.sketchup_model
elements = layer_collection['@elements'] || layer_collection['elements']
name = layer_collection['name']
name = layer_collection['full_path'] if layer_collection['full_path']
# name = layer_collection['name']
# name = layer_collection['full_path'] if layer_collection['full_path']
# name = SketchupModel::Query::Layer.get_last_increment_layer(sketchup_model, name)

layer = sketchup_model.layers.find { |l| l.display_name == name }
# NOTE: this is a v3 way of finding layers since we have incremented layer numberings
layer = sketchup_model.layers.find do |l|
BASE_DICT.get_attribute(l, :speckle_id) == layer_collection['id']
end

# layer = sketchup_model.layers.find { |l| l.display_name == name }
layer_or_folder = layer if layer

elements.each do |element|
Expand Down
Loading