Skip to content

Commit

Permalink
Feat (GIS): support line element
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzhankoral authored Nov 30, 2023
2 parents 60523dc + 2befefa commit 503fb4d
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 17 deletions.
1 change: 1 addition & 0 deletions speckle_connector/src/constants/type_constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module SpeckleConnector
BASE_OBJECT = 'Base'

OBJECTS_GIS_POLYGONELEMENT = 'Objects.GIS.PolygonElement'
OBJECTS_GIS_LINEELEMENT = 'Objects.GIS.LineElement'

OBJECTS_BUILTELEMENTS_VIEW3D = 'Objects.BuiltElements.View:Objects.BuiltElements.View3D'
OBJECTS_BUILTELEMENTS_NETWORK = 'Objects.BuiltElements.Network'
Expand Down
4 changes: 4 additions & 0 deletions speckle_connector/src/convertors/to_native.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_relative '../constants/type_constants'
require_relative '../speckle_entities/speckle_entity'
require_relative '../speckle_objects/gis/polygon_element'
require_relative '../speckle_objects/gis/line_element'
require_relative '../speckle_objects/other/transform'
require_relative '../speckle_objects/other/render_material'
require_relative '../speckle_objects/other/block_definition'
Expand Down Expand Up @@ -65,6 +66,7 @@ def initialize(state, stream_id, stream_name, branch_name, source_app)
DISPLAY_VALUE = OTHER::DisplayValue
VIEW3D = BUILTELEMENTS::View3d
POLYGON_ELEMENT = GIS::PolygonElement
LINE_ELEMENT = GIS::LineElement
COLLECTION = SpeckleObjects::Speckle::Core::Models::Collection
GIS_LAYER_COLLECTION = SpeckleObjects::Speckle::Core::Models::GisLayerCollection

Expand All @@ -86,6 +88,7 @@ def initialize(state, stream_id, stream_name, branch_name, source_app)
Objects.BuiltElements.Wall:Objects.BuiltElements.Revit.RevitWall
Objects.BuiltElements.Network
Objects.GIS.PolygonElement
Objects.GIS.LineElement
Speckle.Core.Models.Collection
Speckle.Core.Models.Collection:Objects.GIS.RasterLayer
Speckle.Core.Models.Collection:Objects.GIS.VectorLayer
Expand Down Expand Up @@ -307,6 +310,7 @@ def speckle_object_to_native(obj)
OBJECTS_BUILTELEMENTS_REVIT_DIRECTSHAPE => BUILTELEMENTS::Revit::DirectShape.method(:to_native),
OBJECTS_BUILTELEMENTS_NETWORK => BUILTELEMENTS::Network.method(:to_native),
OBJECTS_GIS_POLYGONELEMENT => POLYGON_ELEMENT.method(:to_native),
OBJECTS_GIS_LINEELEMENT => LINE_ELEMENT.method(:to_native),
SPECKLE_CORE_MODELS_COLLECTION => COLLECTION.method(:to_native),
SPECKLE_CORE_MODELS_COLLECTION_RASTER_LAYER => GIS_LAYER_COLLECTION.method(:to_native),
SPECKLE_CORE_MODELS_COLLECTION_VECTOR_LAYER => GIS_LAYER_COLLECTION.method(:to_native)
Expand Down
68 changes: 68 additions & 0 deletions speckle_connector/src/speckle_objects/gis/line_element.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

require_relative 'utils'
require_relative '../base'
require_relative '../other/transform'
require_relative '../other/block_definition'
require_relative '../other/block_instance'
require_relative '../../constants/type_constants'
require_relative '../../sketchup_model/dictionary/dictionary_handler'

module SpeckleConnector
module SpeckleObjects
module GIS
# Line element in GIS tools.
class LineElement < Base
SPECKLE_TYPE = OBJECTS_GIS_LINEELEMENT

# Handles polygon element differently from display value.
def self.to_native(state, obj, layer, entities, &convert_to_native)
attributes = GIS.get_qgis_attributes(obj)
obj = collect_definition_geometries(obj)
obj['name'] = GIS.get_definition_name(obj, attributes)

state, _definitions = Other::BlockDefinition.to_native(
state, obj, layer, entities, &convert_to_native
)

definition = state.sketchup_state.sketchup_model
.definitions[Other::BlockDefinition.get_definition_name(obj)]

Other::BlockInstance.find_and_erase_existing_instance(definition, obj['id'], obj['applicationId'])
t_arr = obj['transform']
transform = t_arr.nil? ? Geom::Transformation.new : Other::Transform.to_native(t_arr, obj['units'])
instance = entities.add_instance(definition, transform)
instance.name = obj['name'] unless obj['name'].nil?
SketchupModel::Dictionary::DictionaryHandler.set_hash(instance, attributes, 'qgis')
SketchupModel::Dictionary::DictionaryHandler.set_hash(definition, attributes, 'qgis')
# Align instance axes that created from display value. (without any transform)
Other::BlockInstance.align_instance_axes(instance)
return state, [instance, definition]
end

def self.collect_definition_geometries(obj)
geometries = []

# FIXME: This type check needed because of QGIS. It can send geometries both way, object or array..
# This is something need to be fixed by QGIS.
if obj['geometry'].is_a?(Array)
obj['geometry'].each do |geometry|
geometries << geometry
end
else
geometries += obj['geometry']
end

geometries.each do |geo|
if geo['speckle_type'] && geo['speckle_type'] == OBJECTS_GEOMETRY_MESH
geo['sketchup_attributes'] = { 'is_soften' => false }
end
end

obj['geometry'] = geometries
obj
end
end
end
end
end
20 changes: 3 additions & 17 deletions speckle_connector/src/speckle_objects/gis/polygon_element.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative 'utils'
require_relative '../base'
require_relative '../other/transform'
require_relative '../other/block_definition'
Expand All @@ -14,26 +15,11 @@ module GIS
class PolygonElement < Base
SPECKLE_TYPE = OBJECTS_GIS_POLYGONELEMENT

def self.get_definition_name(obj, attributes)
return obj['name'] unless obj['name'].nil?

return attributes['name'] unless attributes['name'].nil?

return "def::#{obj['id']}"
end

def self.get_qgis_attributes(obj)
attributes = obj['attributes'].to_h
speckle_properties = %w[id speckle_type totalChildrenCount units applicationId]
speckle_properties.each { |key| attributes.delete(key) }
attributes
end

# Handles polygon element differently from display value.
def self.to_native(state, obj, layer, entities, &convert_to_native)
attributes = get_qgis_attributes(obj)
attributes = GIS.get_qgis_attributes(obj)
obj = collect_definition_geometries(obj)
obj['name'] = get_definition_name(obj, attributes)
obj['name'] = GIS.get_definition_name(obj, attributes)

state, _definitions = Other::BlockDefinition.to_native(
state, obj, layer, entities, &convert_to_native
Expand Down
29 changes: 29 additions & 0 deletions speckle_connector/src/speckle_objects/gis/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require_relative '../base'
require_relative '../other/transform'
require_relative '../other/block_definition'
require_relative '../other/block_instance'
require_relative '../../constants/type_constants'
require_relative '../../sketchup_model/dictionary/dictionary_handler'

module SpeckleConnector
module SpeckleObjects
module GIS
def self.get_definition_name(obj, attributes)
return obj['name'] unless obj['name'].nil?

return attributes['name'] unless attributes['name'].nil?

return "def::#{obj['id']}"
end

def self.get_qgis_attributes(obj)
attributes = obj['attributes'].to_h
speckle_properties = %w[id speckle_type totalChildrenCount units applicationId]
speckle_properties.each { |key| attributes.delete(key) }
attributes
end
end
end
end

0 comments on commit 503fb4d

Please sign in to comment.