Compare commits

...

118 Commits

Author SHA1 Message Date
Oğuzhan Koral 78cf7f42e3 Feat (Instancing): Support instancing 2023-03-08 23:56:53 +03:00
oguzhankoral 2b52454c00 Extract collect_definition_geometries for display value 2023-03-08 23:56:10 +03:00
oguzhankoral e032e4561b Receive unique elements with element id even if they have same type 2023-03-08 23:41:42 +03:00
oguzhankoral 23ce1e0476 Make soft edge according to definition name 2023-03-08 23:35:47 +03:00
oguzhankoral 825b7b98d7 Fix rubocop suggestions 2023-03-08 23:35:26 +03:00
oguzhankoral 3de3128a2a Cover mullions by including their elements 2023-03-08 23:34:56 +03:00
oguzhankoral 92d70ccc8b Support Objects.Other.Revit.RevitInstance 2023-03-08 23:33:14 +03:00
oguzhankoral f8c5154457 Remove TransparencySort option from model
- It does not make sense anymore
2023-03-08 23:32:35 +03:00
Oğuzhan Koral 3a3874e9f1 Feat (RevitLevels): Revit levels to Sketchup section planes 2023-03-08 20:31:51 +03:00
oguzhankoral 61b94c519b Update TransparencySort value to 3 by default 2023-03-08 14:39:58 +03:00
oguzhankoral ac0f46ca66 Update unique definitions for revit objects 2023-03-08 14:17:05 +03:00
oguzhankoral e538cdb509 Create levels from objects 2023-03-08 14:16:46 +03:00
oguzhankoral df8a63c5f5 By default set DisplaySectionPlanes is true 2023-03-08 14:16:23 +03:00
oguzhankoral 6827a61bf8 Simplify convert_to_speckle_entities logic 2023-03-08 12:32:52 +03:00
Oğuzhan Koral 20e56c05b3 Feat (Receive): Receive objects as speckle entity 2023-03-07 15:30:59 +03:00
oguzhankoral 5494139319 Disable rubocop issues 2023-03-07 15:28:25 +03:00
oguzhankoral c491ba087a Implement another user setting to activate continuous traversal 2023-03-07 15:17:38 +03:00
oguzhankoral 5cf045c941 Collect speckle materials from sketchup model 2023-03-07 15:17:16 +03:00
oguzhankoral 55fea5f077 Add definitions as collection 2023-03-07 15:16:47 +03:00
oguzhankoral 3a9cb73950 Implement dynamic to_native methods to objects 2023-03-07 15:14:12 +03:00
oguzhankoral d99f613b79 Return combined faces from clean_up 2023-03-07 15:13:33 +03:00
oguzhankoral 68db8ddff3 Add type constants 2023-03-07 15:12:50 +03:00
oguzhankoral c7490b264b Pass stream id to converter by default 2023-03-07 15:11:34 +03:00
Oğuzhan Koral a8e84cfad3 Chore (Query): Query source application from commit 2023-02-28 16:03:56 +03:00
oguzhankoral 5ec4f7e4e8 Pass source application info to command and action 2023-02-28 15:54:04 +03:00
oguzhankoral b019ce2f2e Update stream query for commit sourceApplication 2023-02-28 15:53:41 +03:00
Oğuzhan Koral dd90e41610 Fix (Instance): Component instance naming 2023-02-27 10:43:20 +03:00
oguzhankoral bdf78e374d Update diffing icon with eye instead triangle 2023-02-27 10:27:16 +03:00
oguzhankoral 163197f583 Do not set speckle id as instance name as fallback
Now as is
2023-02-27 10:26:53 +03:00
oguzhankoral 8e13bba44a Write always entity dictionaries while converting
This was tightly coupled with diffing before, now it is separated. It's better.
2023-02-27 10:26:03 +03:00
Oğuzhan Koral e4478b86a5 Feat (SpeckleEntity): Read speckle entities when model loaded 2023-02-27 09:01:12 +03:00
oguzhankoral 1b1ae59ed7 Read speckle entities from sketchup model 2023-02-24 19:18:16 +03:00
oguzhankoral a54f44d22a Update init method of speckle entity with more general way 2023-02-24 19:18:03 +03:00
oguzhankoral d2394274eb Simplify speckle entity object
- So entity specific objects removed
2023-02-24 19:17:18 +03:00
Oğuzhan Koral 0eedb0b397 Chore (Mixpanel): Mixpanel alignment 2023-02-24 10:59:45 +03:00
oguzhankoral 782e9db1f6 Align mixpanel event names 2023-02-24 10:10:50 +03:00
Oğuzhan Koral 2977472c56 Feat (Preferences): Validate model and user preferences 2023-02-22 13:02:18 +03:00
oguzhankoral 8f3a8bfa91 Validate user and model preferences while reading 2023-02-22 12:53:26 +03:00
Oğuzhan Koral 914f6e9516 Fix (Model): Update preferences on state and UI when model has changed 2023-02-21 13:01:53 +03:00
oguzhankoral fa6f65cb7c Use handler methods instead watchers per switch 2023-02-21 12:57:24 +03:00
oguzhankoral dafbe6f995 Update preferences on state and UI when model has changed 2023-02-21 10:47:49 +03:00
Oğuzhan Koral 4a8ac6377b Fix (Preferences): Validate preferences if data is incomplete 2023-02-20 16:47:55 +03:00
oguzhankoral c91fac4ec4 Validate preferences if data is incomplete 2023-02-20 16:30:46 +03:00
Oğuzhan Koral 00d23b084e Chore (Attributes): Send receive attributes as deserialized 2023-02-20 14:58:21 +03:00
oguzhankoral e0a3b82cea Send receive attributes as deserialized 2023-02-20 14:56:01 +03:00
Oğuzhan Koral e49036d8c4 Chore (Attributes): Send and receive ifc classifications 2023-02-20 13:36:52 +03:00
oguzhankoral 4a8356692b Send and receive classifications for component definitions 2023-02-20 13:30:31 +03:00
Oğuzhan Koral adfb7bc63a Fix (BlockDefinition): align detach properties 2023-02-20 12:44:35 +03:00
oguzhankoral 3f41cefa88 Remove basePoint from definition 2023-02-20 12:38:57 +03:00
oguzhankoral 2074ff1987 Rename blockDefinition to definition for BlockInstance 2023-02-20 11:18:50 +03:00
Oğuzhan Koral 65fe189421 Feat (Views): Expand scene support 2023-02-17 22:27:43 +03:00
oguzhankoral bb7590eab4 Simplify sending rendering_options 2023-02-17 22:22:26 +03:00
oguzhankoral 6a04457219 Send and receive rendering options of scenes 2023-02-17 21:51:44 +03:00
oguzhankoral 7f8b1d9586 Include update properties to scenes 2023-02-17 15:39:00 +03:00
Oğuzhan Koral 4362cc53bb Feat (Diffing): MVP for continuous traversal
Fixed issues:
- Origin Point of Components Visible in the Viewer
- Align detach properties with other connectors
- MVP for continuous traversal
- Create SpeckleEntity object as state holder
2023-02-17 02:26:47 +03:00
oguzhankoral 959e6b2a12 Fix rubocop issues 2023-02-17 02:23:27 +03:00
oguzhankoral 2d91070e5a Distint detached properties and dynamically detached properties 2023-02-17 02:17:00 +03:00
oguzhankoral cc038ece32 Remove bbox from mesh 2023-02-17 02:16:59 +03:00
oguzhankoral 7af1292f29 Add setting for diffing 2023-02-17 02:16:59 +03:00
oguzhankoral 2c3fd7a84f Update preferences if empty or has incomplete data 2023-02-17 02:16:59 +03:00
oguzhankoral d36f70dbc7 Align base object serializer with continuous traversal 2023-02-17 02:16:59 +03:00
oguzhankoral 77f5f29c90 Rename batch logging method 2023-02-17 02:16:58 +03:00
oguzhankoral ead45ed843 Remove applicationId from clean base object before traverse props 2023-02-17 02:16:58 +03:00
oguzhankoral a6259bb7eb Print on Speckle folder 2023-02-17 02:16:58 +03:00
oguzhankoral 0249ebeb95 Move is_detached to old order 2023-02-17 02:16:57 +03:00
oguzhankoral 1c3a35a137 Fill delta icon if diffing active on stream 2023-02-17 02:16:57 +03:00
oguzhankoral c12319b417 Activate and deactivate diffing for streams safely 2023-02-17 02:16:57 +03:00
oguzhankoral 90ac00a628 Pass stream if to speckle entities 2023-02-17 02:16:57 +03:00
oguzhankoral e5c71cb3a4 Handle speckle entity dictionaries according validation of object 2023-02-17 02:16:56 +03:00
oguzhankoral ebf0681574 Collect invalid streams from speckle_state 2023-02-17 02:16:56 +03:00
oguzhankoral 642643f412 Remove unnecssary implementation for entity utis 2023-02-17 02:16:56 +03:00
oguzhankoral 8dd65ac255 Init diffing materials with plugin initialization 2023-02-17 02:16:55 +03:00
oguzhankoral 8df58f226b Invalida speckle entities when they edited 2023-02-17 02:16:55 +03:00
oguzhankoral 58b4f2ce14 Disable observers on commands 2023-02-17 02:16:55 +03:00
oguzhankoral 65f0c836fe Align objects with continuous traversal 2023-02-17 02:16:55 +03:00
oguzhankoral 2aa16085a5 Return converted entities regardless 2023-02-17 02:16:54 +03:00
oguzhankoral 25f05a69c6 Return new speckle_state from serializer 2023-02-17 02:16:54 +03:00
oguzhankoral 5c2a94da16 Align speckle entities with traversed objects 2023-02-17 02:16:54 +03:00
oguzhankoral 162325e90e Add desktop path to constants 2023-02-17 02:16:54 +03:00
oguzhankoral b022a8c608 Implement observers 2023-02-17 02:16:53 +03:00
oguzhankoral 1e404f5e6b Return converted and traversed versions from entities 2023-02-17 02:16:53 +03:00
oguzhankoral 8ef7780332 Write application_id to dictionary of SpeckleEntity 2023-02-17 02:16:53 +03:00
oguzhankoral 51b4f7b3f7 Use definition persistent id for nested block definitions 2023-02-17 02:16:53 +03:00
oguzhankoral 9b3fa33e50 Save traversed object details into SpeckleEntity's dictionary 2023-02-17 02:16:52 +03:00
oguzhankoral 9349d0813d Implement speckle_block_instance_entity 2023-02-17 02:16:52 +03:00
oguzhankoral 8d9bc500a1 Write traversed info to speckle entity's dictionary 2023-02-17 02:16:52 +03:00
oguzhankoral cae7b6e29f Add line and mesh speckle entities 2023-02-17 02:16:51 +03:00
oguzhankoral 0b4ac732b2 Fix traversal missing references 2023-02-17 02:16:51 +03:00
oguzhankoral acacbb91e3 Add single unit test for line traversing 2023-02-17 02:16:51 +03:00
oguzhankoral fd6d3d9a2f Pass state to converters 2023-02-17 02:16:51 +03:00
oguzhankoral 3b531e30b1 Disable caching temporarly 2023-02-17 02:16:50 +03:00
oguzhankoral 0c78085b2e Remove command data from console logging 2023-02-17 02:16:50 +03:00
oguzhankoral 4ab53308f7 Initialize relations on to_speckle 2023-02-17 02:16:50 +03:00
oguzhankoral 06ae161793 Check base object already traversed or not 2023-02-17 02:16:50 +03:00
oguzhankoral 94e005d2f8 Fix speckle_state immutable update on state 2023-02-17 02:16:49 +03:00
oguzhankoral 717072c3a5 Introduce speckle_entity objects 2023-02-17 02:16:49 +03:00
oguzhankoral 02b4bde92a Add speckle state for serialization process 2023-02-17 02:16:49 +03:00
oguzhankoral a397d1e233 Return speckle state from traversal methods 2023-02-17 02:16:45 +03:00
Oğuzhan Koral d2198c0765 Improve refreshMixpanelIds function with better registration 2023-02-17 02:16:28 +03:00
oguzhankoral 549bf63198 Improve refreshMixpanelIds function with better registration and identifying 2023-02-16 16:26:00 +03:00
Oğuzhan Koral 8b824f5342 Update innosetup workflow
There were issues with innosetup workflow which does not run one powershell.exe,
so workflows splitted into chunks according to shells
2023-02-08 21:20:17 +03:00
oguzhankoral d052d5e8a1 Change set env variable to run with powershell 2023-02-08 21:12:00 +03:00
oguzhankoral 610c22dd02 Update innosetup workflow 2023-02-08 21:05:02 +03:00
Oğuzhan Koral a5496ab6a9 Feat (CI): Run CI step to create env variables for innosetup 2023-02-08 20:14:23 +03:00
oguzhankoral c29c8f009c Remove duplicated context: innosetup on yaml file 2023-02-02 10:34:34 +02:00
oguzhankoral fb7e9f2a6c Run CI step to create env variables for innosetup 2023-02-01 23:42:25 +02:00
Oğuzhan Koral 62c2bbb9fa Chore (Notification): Add error notification to GlobalToast 2023-01-23 15:34:03 +03:00
oguzhankoral 7656772194 Add error notification to GlobalToast 2023-01-23 15:31:13 +03:00
Oğuzhan Koral f22ff050e0 Fix (Stream, Attributes): Follow up fixes add stream by url and attributes 2023-01-23 12:35:22 +03:00
oguzhankoral 708f0b44fd Check face entity attributes settings before merging faces into mesh 2023-01-23 12:01:47 +03:00
oguzhankoral 2ee4581f17 Check stream role is null first before split 2023-01-23 12:00:13 +03:00
Oğuzhan Koral e337fb869f Feat (Settings) Advanced settings for entity specific attributes 2023-01-22 00:09:05 +03:00
oguzhankoral 4a8b0147e1 Create UI components for entity specific settings 2023-01-22 00:03:18 +03:00
oguzhankoral c95a1c7e1f Consider entity settings on speckle objects 2023-01-22 00:02:51 +03:00
oguzhankoral a07cd5c3f5 Extend model preferences with entity specific settings 2023-01-22 00:02:24 +03:00
Oğuzhan Koral 4d1473582e Feat (Stream): Add streams by URL or id 2023-01-21 17:33:35 +03:00
oguzhankoral 3bc9f4c452 Fix rubocop issue 2023-01-21 17:33:15 +03:00
oguzhankoral 11377038a0 Implement and make functional adding streams by url 2023-01-20 22:26:36 +03:00
84 changed files with 2702 additions and 619 deletions
+33 -11
View File
@@ -36,19 +36,41 @@ jobs:
- attach_workspace:
at: ./
- run:
name: Patch
name: Create Innosetup signing cert
shell: powershell.exe
command:
| # If no tag, use 0.0.0.1 and don't make any YML (for testing only!)
$tag = if([string]::IsNullOrEmpty($env:CIRCLE_TAG)) { "0.0.0" } else { $env:CIRCLE_TAG }
$semver = if($tag.Contains('/')) {$tag.Split("/")[1] } else { $tag }
command: |
echo $env:PFX_B64 > "speckle-sharp-ci-tools\SignTool\AEC Systems Ltd.txt"
certutil -decode "speckle-sharp-ci-tools\SignTool\AEC Systems Ltd.txt" "speckle-sharp-ci-tools\SignTool\AEC Systems Ltd.pfx"
- run:
name: Set Environment Variable
shell: powershell.exe
command: |
$tag = if([string]::IsNullOrEmpty($env:CIRCLE_TAG)) { "2.0.999" } else { $env:CIRCLE_TAG }
$semver = if($tag.Contains('/')) {$tag.Split("/")[0] } else { $tag }
$ver = if($semver.Contains('-')) {$semver.Split("-")[0] } else { $semver }
$channel = if($semver.Contains('-')) {$semver.Split("-")[1] } else { "latest" }
$version = "$($ver).$($env:CIRCLE_BUILD_NUM)"
New-Item -Force "speckle-sharp-ci-tools/Installers/sketchup/$channel.yml" -ItemType File -Value "version: $semver"
echo $version
$version = "$($ver).$($env:WORKFLOW_NUM)"
python patch_version.py $semver
speckle-sharp-ci-tools\InnoSetup\ISCC.exe speckle-sharp-ci-tools\sketchup.iss
environment:
WORKFLOW_NUM: << pipeline.number >>
- run:
name: Build Installer
command: speckle-sharp-ci-tools\InnoSetup\ISCC.exe speckle-sharp-ci-tools\sketchup.iss /Sbyparam=$p
shell: cmd.exe #does not work in powershell
#- run:
# name: Patch
# shell: powershell.exe
# command:
# | # If no tag, use 0.0.0.1 and don't make any YML (for testing only!)
# $tag = if([string]::IsNullOrEmpty($env:CIRCLE_TAG)) { "0.0.0" } else { $env:CIRCLE_TAG }
# $semver = if($tag.Contains('/')) {$tag.Split("/")[1] } else { $tag }
# $ver = if($semver.Contains('-')) {$semver.Split("-")[0] } else { $semver }
# $channel = if($semver.Contains('-')) {$semver.Split("-")[1] } else { "latest" }
# $version = "$($ver).$($env:CIRCLE_BUILD_NUM)"
# New-Item -Force "speckle-sharp-ci-tools/Installers/sketchup/$channel.yml" -ItemType File -Value "version: $semver"
# echo $version
# python patch_version.py $semver
# speckle-sharp-ci-tools\InnoSetup\ISCC.exe speckle-sharp-ci-tools\sketchup.iss
- persist_to_workspace:
root: ./
paths:
@@ -115,7 +137,6 @@ workflows:
only: /.*/
- build-connector:
context: innosetup
slug: sketchup
requires:
- get-ci-tools
@@ -123,6 +144,7 @@ workflows:
filters:
tags:
only: /.*/
context: innosetup
- deploy-manager2:
context: do-spaces-speckle-releases
@@ -0,0 +1,33 @@
# frozen_string_literal: true
require_relative 'action'
require_relative 'deactivate_diffing'
module SpeckleConnector
module Actions
# Deactivate diffing for stream.
class ActivateDiffing < Action
def initialize(stream_id)
super()
@stream_id = stream_id
end
# @param state [States::State] the current state of the {App::SpeckleConnectorApp}
# @return [States::State] the new updated state object
def update_state(state)
state = DeactivateDiffing.update_state(state, {})
puts "Diffing activated for #{@stream_id}"
speckle_entities = state.speckle_state.speckle_entities
invalid_speckle_entities = speckle_entities.select do |_id, entity|
entity.invalid_stream_ids.include?(@stream_id) && entity.sketchup_entity.is_a?(Sketchup::Face)
end
invalid_speckle_entities.each do |id, entity|
new_entity = entity.activate_diffing(@stream_id, state.sketchup_state.materials.by_id(MAT_EDIT))
speckle_entities = speckle_entities.put(id, new_entity)
end
new_speckle_state = state.speckle_state.with_speckle_entities(speckle_entities)
state.with_speckle_state(new_speckle_state)
end
end
end
end
@@ -0,0 +1,52 @@
# frozen_string_literal: true
require_relative 'action'
module SpeckleConnector
module Actions
# Adds material to speckle state and Sketchup.
class AddMaterial < Action
def self.update_state(state, material_name:, color:, material_id:, alpha: nil)
materials = state.sketchup_state.materials
existing_material = materials.by_id(material_id)
return state if existing_material&.valid?
new_material = create_or_get_material(state.sketchup_state.sketchup_model,
material_name,
color,
material_id,
alpha: alpha)
new_materials = materials.add_material(material_id, new_material)
new_sketchup_state = state.sketchup_state.with(:@materials => new_materials)
state.with(:@sketchup_state => new_sketchup_state)
end
def self.create_or_get_material(model, material_name, color, material_id, alpha: nil)
materials = model.materials
existing_material = materials.find { |mat| mat.name == material_name }
return existing_material if existing_material&.valid?
existing_material = materials.add material_name
existing_material.set_attribute(MAT_DICTIONARY, MAT_ID, material_id.to_s)
set_hex_color(existing_material, color)
existing_material.alpha = alpha if alpha
existing_material
end
def self.set_hex_color(skp_material, hex_value)
hex_value = hex_value.to_s
col_blue, col_green, col_red = parse_hex_color(hex_value)
skp_material.color = col_red, col_green, col_blue
end
def self.parse_hex_color(hex_value)
split_values = hex_value.match(/^#([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})$/) ||
hex_value.match(/^#([a-fA-F\d])([a-fA-F\d])([a-fA-F\d])$/)
col_red = split_values[1].hex
col_green = split_values[2].hex
col_blue = split_values[3].hex
return col_blue, col_green, col_red
end
end
end
end
@@ -0,0 +1,20 @@
# frozen_string_literal: true
require_relative 'action'
module SpeckleConnector
module Actions
# Action to collect versions from sketchup and connector to track user's version by mixpanel.
class CollectVersions < Action
# @param state [States::State] the current state of the {App::SpeckleConnectorApp}
# @return [States::State] the new updated state object
def self.update_state(state, _data)
versions = {
sketchup: Sketchup.version.to_i,
speckle: SpeckleConnector::CONNECTOR_VERSION
}
state.with_add_queue('collectVersions', versions.to_json, [])
end
end
end
end
@@ -0,0 +1,26 @@
# frozen_string_literal: true
require_relative 'action'
module SpeckleConnector
module Actions
# Deactivate diffing.
class DeactivateDiffing < Action
# @param state [States::State] the current state of the {App::SpeckleConnectorApp}
# @return [States::State] the new updated state object
def self.update_state(state, _data)
puts 'Diffing deactivated!'
speckle_entities = state.speckle_state.speckle_entities
diffing_activated_speckle_entities = speckle_entities.reject do |_id, entity|
entity.active_diffing_stream_id.nil?
end
diffing_activated_speckle_entities.each do |id, entity|
new_entity = entity.deactivate_diffing
speckle_entities = speckle_entities.put(id, new_entity)
end
new_speckle_state = state.speckle_state.with_speckle_entities(speckle_entities)
state.with_speckle_state(new_speckle_state)
end
end
end
end
@@ -0,0 +1,56 @@
# frozen_string_literal: true
require_relative 'event_action'
require_relative '../load_sketchup_model'
require_relative '../collect_preferences'
module SpeckleConnector
module Actions
module Events
# Handle events that are triggered by the {AppObserver}.
class AppEventAction < EventAction
# Handle loading new or existing model
class OnNewOrChangedModel
# Handle events when the new or existing model is loaded in Sketchup
# @param state [States::State] the current state of speckle application
# @param event_data [Array<(Sketchup::Model)>] the event data for the given event. It consists of
# a double array with a single element that is the {Sketchup::Model} object of the loaded model.
def self.update_state(state, event_data)
return state unless event_data&.any?
model = event_data.flatten.first
# LoadSketchupModel action should be responsible to update all model specific data for state and then
# should notify the UI to update it's components.
new_state = Actions::LoadSketchupModel.update_state(state, model)
# Action to let UI to render itself with new preferences state
# TODO: Later UI should be updated if any stream is invalid after
# we collected speckle_entities appropriately
CollectPreferences.update_state(new_state, {})
end
end
# Run actions that are needed before the sketchup quits
class OnQuit
# Handle when Sketchup application closes
# @param state [States::State] the current state of speckle application
# @param _event_data [Array] the event data
# @return [States::State] the transformed state object
def self.update_state(state, _event_data)
state
end
end
# Handlers that are used to handle specific events
ACTIONS = {
onNewModel: OnNewOrChangedModel,
onOpenModel: OnNewOrChangedModel,
onQuit: OnQuit
}.freeze
def self.actions
ACTIONS
end
end
end
end
end
@@ -0,0 +1,84 @@
# frozen_string_literal: true
require_relative 'event_action'
require_relative '../../sketchup_model/utils/face_utils'
require_relative '../../constants/dict_constants'
module SpeckleConnector
module Actions
module Events
# Event actions related to entities.
class EntitiesEventAction < EventAction
# Event action when element added.
class OnElementAdded
# @param state [States::State] the current state of the SpeckleConnector Application
def self.update_state(state, event_data)
modified_entities = event_data.to_a.collect { |e| e[1] }
# do not copy speckle base object specific attributes, because they are entity specific
modified_entities.each { |entity| entity.delete_attribute(SPECKLE_BASE_OBJECT) }
state
end
end
# Event action when element modified.
class OnElementModified
# @param state [States::State] the current state of the SpeckleConnector Application
def self.update_state(state, event_data)
speckle_state = state.speckle_state
modified_entity = event_data[0][1]
if modified_entity.is_a?(Sketchup::Face)
path = state.sketchup_state.sketchup_model.active_path
modified_faces = SketchupModel::Utils::FaceUtils.near_faces(modified_entity.edges)
path_objects = path.nil? ? [] : path + path.collect(&:definition)
parent_ids = path_objects.collect(&:persistent_id)
ids_to_invalidate = modified_faces.collect(&:persistent_id) + parent_ids
entities_to_invalidate = speckle_entities_to_invalidate(speckle_state, ids_to_invalidate)
new_speckle_state = invalidate_speckle_entities(speckle_state, entities_to_invalidate)
# This is the place we can send information to UI for diffing check
diffing = state.user_state.preferences[:user][:diffing]
new_speckle_state = new_speckle_state.with_invalid_streams_queue if diffing
return state.with_speckle_state(new_speckle_state)
end
state
end
# @param speckle_state [States::SpeckleState] the current state of the Speckle
def self.speckle_entities_to_invalidate(speckle_state, ids)
speckle_state.speckle_entities.to_h.select { |id, _| ids.include?(id) }
end
# @param speckle_state [States::SpeckleState] the current state of the Speckle
def self.invalidate_speckle_entities(speckle_state, entities_to_invalidate)
speckle_entities = speckle_state.speckle_entities
entities_to_invalidate.each do |id, speckle_entity|
edited_speckle_entity = speckle_entity.with_invalid
speckle_entities = speckle_entities.put(id, edited_speckle_entity)
end
speckle_state.with_speckle_entities(speckle_entities)
end
end
# Event action when element removed.
class OnElementRemoved
# @param state [States::State] the current state of the SpeckleConnector Application
def self.update_state(state, _event_data)
# TODO: Do state updates when element removed
state
end
end
# Handlers that are used to handle specific events
ACTIONS = {
onElementRemoved: OnElementRemoved,
onElementAdded: OnElementAdded,
onElementModified: OnElementModified
}.freeze
def self.actions
ACTIONS
end
end
end
end
end
@@ -0,0 +1,34 @@
# frozen_string_literal: true
module SpeckleConnector
module Actions
# This module contains actions that are performed to handle events triggered by observers in Sketchup.
module Events
# Base action for Handling events
class EventAction
def self.actions
raise NoMethodError, 'Implement in a subclass'
end
# Handle the events that were collected by the observer. In case of the selection observer,
# we only need to handle the events once if any of the events actually happened.
# @param state [States::State] the current state of the SpeckleConnector Application
# @param events [Hash{Symbol=>Array}] the event data grouped by the event name
# @return [States::State] the transformed state
def self.update_state(state, events)
# Don't do anything if there are no events for this action
return state unless events
actions = self.actions
actions.each do |event_name, action|
next unless events.key?(event_name)
event_data = events[event_name]
state = action.update_state(state, event_data)
end
state
end
end
end
end
end
@@ -0,0 +1,41 @@
# frozen_string_literal: true
require_relative 'event_action'
require_relative '../load_sketchup_model'
module SpeckleConnector
module Actions
module Events
# Handle events that are triggered by the {ModelObserver}.
class ModelEventAction < EventAction
# Handle loading new or existing model
class OnActivePathChanged
# Handle events when the new or existing model is loaded in Sketchup
# @param state [States::State] the current state of speckle application
# @param event_data [Array<(Sketchup::Model)>] the event data for the given event. It consists of
# a double array with a single element that is the {Sketchup::Model} object of the loaded model.
def self.update_state(state, _event_data)
sketchup_state = state.sketchup_state
active_path = sketchup_state.sketchup_model.active_path
observers = state.speckle_state.observers
update_object_observers(active_path, observers)
return state
end
def self.update_object_observers(path, observers)
path[-1].definition.entities.add_observer(observers[ENTITIES_OBSERVER]) unless path.nil?
end
end
# Handlers that are used to handle specific events
ACTIONS = {
onActivePathChanged: OnActivePathChanged
}.freeze
def self.actions
ACTIONS
end
end
end
end
end
@@ -0,0 +1,30 @@
# frozen_string_literal: true
require_relative 'action'
require_relative 'add_material'
require_relative '../constants/mat_constants'
module SpeckleConnector
module Actions
# Action to initialize materials
class InitializeMaterials < Action
# @param state [States::State] the current state of the {App::SpeckleConnectorApp}
# @return [States::State] the new updated state object
def self.update_state(state)
new_state = recreate_material(state, DEFAULT_NAMES[MAT_ADD], DEFAULT_COLORS[MAT_ADD], MAT_ADD)
new_state = recreate_material(new_state, DEFAULT_NAMES[MAT_EDIT], DEFAULT_COLORS[MAT_EDIT], MAT_EDIT)
recreate_material(new_state, DEFAULT_NAMES[MAT_REMOVE], DEFAULT_COLORS[MAT_REMOVE], MAT_REMOVE)
end
def self.recreate_material(state, name, color, id, alpha: nil)
Actions::AddMaterial.update_state(
state,
material_name: name,
color: color,
material_id: id,
alpha: alpha
)
end
end
end
end
@@ -6,6 +6,7 @@ require_relative '../states/speckle_state'
require_relative '../states/sketchup_state'
require_relative '../accounts/accounts'
require_relative '../preferences/preferences'
require_relative '../constants/observer_constants'
module SpeckleConnector
module Actions
@@ -13,14 +14,20 @@ module SpeckleConnector
class InitializeSpeckle < Action
# @param state [States::State] the current state of the {App::SpeckleConnectorApp}
# @return [States::State] the new updated state object
def self.update_state(state)
def self.update_state(state, observers)
attach_app_observer!(observers[APP_OBSERVER])
accounts = SpeckleConnector::Accounts.load_accounts
speckle_state = States::SpeckleState.new(accounts, {}, {})
speckle_state = States::SpeckleState.new(accounts, observers, {}, {})
# This should be the only point that `Sketchup_active_model` passed to application state.
sketchup_state = States::SketchupState.new(Sketchup.active_model)
preferences = Preferences.init_preferences(sketchup_state.sketchup_model)
preferences = Preferences.read_preferences(sketchup_state.sketchup_model)
user_state_with_preferences = state.user_state.with_preferences(preferences)
States::State.new(user_state_with_preferences, speckle_state, sketchup_state, false)
state = States::State.new(user_state_with_preferences, speckle_state, sketchup_state, false)
Actions::LoadSketchupModel.update_state(state, sketchup_state.sketchup_model)
end
def self.attach_app_observer!(observer)
Sketchup.add_observer(observer)
end
end
end
@@ -9,7 +9,8 @@ module SpeckleConnector
# @param state [States::State] the current state of the {App::SpeckleConnectorApp}
# @return [States::State] the new updated state object
def self.update_state(state, _data)
(saved_streams = state.sketchup_state.sketchup_model.attribute_dictionary('speckle', true)['streams']) or []
(saved_streams = state.sketchup_state.sketchup_model
.attribute_dictionary('Speckle', true)['saved_streams']) or []
state.with_add_queue('setSavedStreams', saved_streams, [])
end
end
@@ -0,0 +1,59 @@
# frozen_string_literal: true
require_relative 'action'
require_relative 'initialize_materials'
require_relative '../sketchup_model/reader/speckle_entities_reader'
require_relative '../preferences/preferences'
require_relative '../states/state'
require_relative '../states/sketchup_state'
require_relative '../constants/observer_constants'
module SpeckleConnector
module Actions
# Switch sketchup model wit a new one
class LoadSketchupModel < Action
# Replace current model state with the state of a new model. This action is triggered when user opens new or
# existing Sketchup model.
# @param state [States::State] the current state of Speckle
# @param additional_parameters [Array] parameters that the action takes
# @return [States::State] the new updated state object
def self.update_state(state, sketchup_model)
# Init sketchup state again with new model
new_sketchup_state = States::SketchupState.new(sketchup_model)
sketchup_model.rendering_options['DisplaySectionPlanes'] = true
new_state = state.with(:@sketchup_state => new_sketchup_state)
# Init materials again
new_state = InitializeMaterials.update_state(new_state)
# Read speckle entities
new_speckle_entities = SketchupModel::Reader::SpeckleEntitiesReader.read(sketchup_model.entities)
new_speckle_state = new_state.speckle_state.with_speckle_entities(Immutable::Hash.new(new_speckle_entities))
new_state = new_state.with_speckle_state(new_speckle_state)
# Read preferences from database and model.
preferences = Preferences.read_preferences(new_state.sketchup_state.sketchup_model)
new_user_state = new_state.user_state.with_preferences(preferences)
new_state = new_state.with(:@user_state => new_user_state)
attach_observers(sketchup_model, new_state.speckle_state.observers)
new_state
end
# Attach observers to the sketchup model
# @param sketchup_model [Sketchup::Model] the model to attach observers to
# @param observers [Hash{Class=>}] the observer objects indexed by their class that will be attached
def self.attach_observers(sketchup_model, observers)
# selection = sketchup_model.selection
# selection.add_observer(observers[SELECTION_OBSERVER_NAME])
# layers = sketchup_model.layers
# layers.add_observer(observers[LAYERS_OBSERVER_NAME])
entities = sketchup_model.entities
entities.add_observer(observers[ENTITIES_OBSERVER])
sketchup_model.add_observer(observers[MODEL_OBSERVER])
# materials = sketchup_model.materials
# materials.add_observer(observers[MATERIALS_OBSERVER_NAME])
# pages = sketchup_model.pages
# pages.add_observer(observers[PAGES_OBSERVER_NAME])
end
end
end
end
@@ -0,0 +1,34 @@
# frozen_string_literal: true
require_relative 'action'
require_relative 'events/app_event_action'
require_relative 'events/entities_event_action'
require_relative 'events/model_event_action'
require_relative '../constants/observer_constants'
module SpeckleConnector
module Actions
# Handle events that were collected by observers
class OnEventsAction < Action
RUN_ORDER = {
APP_OBSERVER => Events::AppEventAction,
ENTITIES_OBSERVER => Events::EntitiesEventAction,
MODEL_OBSERVER => Events::ModelEventAction
# MATERIALS_OBSERVER => Events::MaterialsEventAction,
# LAYERS_OBSERVER => Events::LayerEventAction,
# PAGES_OBSERVER => Events::PagesEventAction,
# SELECTION_OBSERVER => Events::SelectionEventAction
}.freeze
def self.update_state(state, events)
RUN_ORDER.each do |observer_name, action|
next unless events.key?(observer_name)
parameters = events[observer_name]
state = action.update_state(state, parameters)
end
state
end
end
end
end
@@ -8,24 +8,28 @@ module SpeckleConnector
module Actions
# Action to receive objects from Speckle Server.
class ReceiveObjects < Action
def initialize(stream_id, base, stream_name, branch_name, branch_id)
# rubocop:disable Metrics/ParameterLists
def initialize(stream_id, base, stream_name, branch_name, branch_id, source_app)
super()
@stream_id = stream_id
@base = base
@stream_name = stream_name
@branch_name = branch_name
@branch_id = branch_id
@source_app = source_app
end
# rubocop:enable Metrics/ParameterLists
# @param state [States::State] the current state of the {App::SpeckleConnectorApp}
# @return [States::State] the new updated state object
def update_state(state)
converter = Converters::ToNative.new(state.sketchup_state.sketchup_model)
converter = Converters::ToNative.new(state, @stream_id)
# Have side effects on the sketchup model. It effects directly on the entities by adding new objects.
start_time = Time.now.to_f
converter.receive_commit_object(@base, state.user_state.preferences[:model])
state = converter.receive_commit_object(@base)
elapsed_time = (Time.now.to_f - start_time).round(3)
puts "==== Converting to Native executed in #{elapsed_time} sec ===="
puts "==== Source application is #{@source_app}. ===="
state.with_add_queue('finishedReceiveInSketchup', @stream_id, [])
end
end
@@ -18,10 +18,10 @@ module SpeckleConnector
# @param state [States::State] the current state of the {App::SpeckleConnectorApp}
# @return [States::State] the new updated state object
def update_state(state)
speckle_dict = state.sketchup_state.sketchup_model.attribute_dictionary('speckle', true)
saved = speckle_dict['streams'] || []
speckle_dict = state.sketchup_state.sketchup_model.attribute_dictionary('Speckle', true)
saved = speckle_dict['saved_streams'] || []
saved -= [@stream_id]
speckle_dict['streams'] = saved
speckle_dict['saved_streams'] = saved
state
end
end
+3 -3
View File
@@ -16,10 +16,10 @@ module SpeckleConnector
# @param state [States::State] the current state of the {App::SpeckleConnectorApp}
# @return [States::State] the new updated state object
def update_state(state)
speckle_dict = state.sketchup_state.sketchup_model.attribute_dictionary('speckle', true)
saved = speckle_dict['streams'] || []
speckle_dict = state.sketchup_state.sketchup_model.attribute_dictionary('Speckle', true)
saved = speckle_dict['saved_streams'] || []
saved = saved.empty? ? [@stream_id] : saved.unshift(@stream_id)
speckle_dict['streams'] = saved
speckle_dict['saved_streams'] = saved
state
end
end
@@ -16,11 +16,10 @@ module SpeckleConnector
# @return [States::State] the new updated state object
def update_state(state)
to_send_stream_id = state.speckle_state.stream_queue[:stream_id]
return state if to_send_stream_id == @stream_id
return state if to_send_stream_id == @stream_id || to_send_stream_id.nil?
to_send_converted = state.speckle_state.stream_queue[:converted].to_json
new_state = state.with_add_queue('convertedFromSketchup', to_send_stream_id, [to_send_converted])
new_state = new_state.with_add_queue('oneClickSend', to_send_stream_id, [])
new_state.with_empty_stream_queue
end
end
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require_relative 'action'
require_relative 'deactivate_diffing'
require_relative '../convertors/units'
require_relative '../convertors/to_speckle'
@@ -16,16 +17,23 @@ module SpeckleConnector
# @param state [States::State] the current state of the {App::SpeckleConnectorApp}
# @return [States::State] the new updated state object
def update_state(state)
sketchup_model = state.sketchup_state.sketchup_model
converter = Converters::ToSpeckle.new(sketchup_model)
base = converter.convert_selection_to_base(state.user_state.preferences)
id, total_children_count, batches = converter.send_info(base)
state = DeactivateDiffing.update_state(state, {})
converter = Converters::ToSpeckle.new(state, @stream_id)
new_speckle_state, base = converter.convert_selection_to_base(state.user_state.preferences)
id, total_children_count, batches, new_speckle_state = converter.serialize(base, new_speckle_state,
state.user_state.preferences)
puts("converted #{base.count} objects for stream #{@stream_id}")
state.with_add_queue('convertedFromSketchup', @stream_id, [
{ is_string: false, val: batches },
{ is_string: true, val: id },
{ is_string: false, val: total_children_count }
])
# This is the place we can send information to UI for diffing check
diffing = state.user_state.preferences[:user][:diffing]
new_speckle_state = new_speckle_state.with_invalid_streams_queue if diffing
new_state = state.with_speckle_state(new_speckle_state)
new_state.with_add_queue('convertedFromSketchup', @stream_id, [
{ is_string: false, val: batches },
{ is_string: true, val: id },
{ is_string: false, val: total_children_count }
])
end
end
end
@@ -18,6 +18,8 @@ module SpeckleConnector
# @param state [States::State] the current state of the {App::SpeckleConnectorApp}
# @return [States::State] the new updated state object
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
def update_state(state)
# Init sqlite database
db = Sqlite3::Database.new(SPECKLE_CONFIG_DB_PATH)
@@ -46,8 +48,19 @@ module SpeckleConnector
user[@preference.to_sym] = @value
new_preferences = state.user_state.preferences.put(:user, user)
new_user_state = state.user_state.with_preferences(new_preferences)
# This is the place we can send information to UI for diffing check. It is a technical depth!
if @preference == 'diffing'
new_speckle_state = if @value
state.speckle_state.with_invalid_streams_queue
else
state.speckle_state.with_empty_invalid_streams_queue
end
state = state.with_speckle_state(new_speckle_state)
end
state.with_user_state(new_user_state)
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
end
end
end
@@ -15,6 +15,9 @@ module SpeckleConnector
# @return [Ui::UiController] controller for ui views
attr_reader :ui_controller
# @return [Observers::Handler] the observers indexed by their classes to handle
attr_reader :observer_handler
def initialize(menu_commands, state, ui_controller)
@menu_commands = menu_commands
@state = state
@@ -35,6 +38,10 @@ module SpeckleConnector
update_state!(Actions::ClearQueue)
end
def add_observer_handler!(observer_handler)
@observer_handler = observer_handler
end
def update_state!(action, *parameters)
old_state = @state
@state = action.update_state(old_state, *parameters)
@@ -0,0 +1,17 @@
# frozen_string_literal: true
require_relative 'command'
require_relative '../actions/activate_diffing'
module SpeckleConnector
module Commands
# Command to activate diffing for stream.
class ActivateDiffing < Command
def _run(data)
stream_id = data['stream_id']
action = Actions::ActivateDiffing.new(stream_id)
app.update_state!(action)
end
end
end
end
+12 -1
View File
@@ -18,11 +18,22 @@ module SpeckleConnector
def run(*parameters)
# Run here common operations that same for each command.
_run(*parameters)
with_observers_disabled do
_run(*parameters)
end
end
private
def with_observers_disabled(&block)
observer_handler = @app.observer_handler
if observer_handler
observer_handler.with_observers_disabled(&block)
else
block.call
end
end
def _run(*_parameters)
raise NotImplementedError, 'Implement in subclass'
end
@@ -4,6 +4,7 @@ require_relative 'command'
require_relative '../states/initial_state'
require_relative '../ui/vue_view'
require_relative '../actions/initialize_speckle'
require_relative '../observers/factory'
module SpeckleConnector
module Commands
@@ -30,7 +31,10 @@ module SpeckleConnector
# Do the actual Speckle initialization.
def initialize_speckle(app)
# TODO: Initialize here speckle states and observers.
app.update_state!(Actions::InitializeSpeckle)
observer_handler = Observers::Factory.create_handler(app)
app.add_observer_handler!(observer_handler)
observers = Observers::Factory.create_observers(observer_handler)
app.update_state!(Actions::InitializeSpeckle, observers)
dialog_specs = {
dialog_id: Ui::SPECKLE_UI_ID,
htm_file: Ui::VUE_UI_HTML,
@@ -13,7 +13,8 @@ module SpeckleConnector
branch_name = data['branch_name']
branch_id = data['branch_id']
stream_name = data['stream_name']
action = Actions::ReceiveObjects.new(stream_id, base, stream_name, branch_name, branch_id)
source_app = data['source_app']
action = Actions::ReceiveObjects.new(stream_id, base, stream_name, branch_name, branch_id, source_app)
app.update_state!(action)
end
end
@@ -1,8 +1,13 @@
# frozen_string_literal: true
module SpeckleConnector
SPECKLE_BASE_OBJECT = 'Speckle_Base_Object'
SPECKLE_ID = 'speckle_id'
SPECKLE_TYPE = 'speckle_type'
APPLICATION_ID = 'application_id'
TOTAL_CHILDREN_COUNT = 'total_children_count'
CHILDREN = 'children'
PARENT = 'parent'
VALID_STREAM_IDS = 'valid_stream_ids'
INVALID_STREAM_IDS = 'invalid_stream_ids'
end
@@ -0,0 +1,22 @@
# frozen_string_literal: true
module SpeckleConnector
MAT_DICTIONARY = 'Speckle_Connector_Materials'
MAT_ID = 'Speckle_Connector_Material_Id'
MAT_ADD = :speckle_connector_add_material
MAT_EDIT = :speckle_connector_edit_material
MAT_REMOVE = :speckle_connector_remove_material
DEFAULT_COLORS = {
MAT_ADD => '#66FF66',
MAT_EDIT => '#FFFF9F',
MAT_REMOVE => '#FF6666'
}.freeze
DEFAULT_NAMES = {
MAT_ADD => 'Speckle_Material_Add',
MAT_EDIT => 'Speckle_Material_Edit',
MAT_REMOVE => 'Speckle_Material_Remove'
}.freeze
end
@@ -0,0 +1,7 @@
# frozen_string_literal: true
module SpeckleConnector
APP_OBSERVER = 'SpeckleConnector::Observers::AppObserver'
ENTITIES_OBSERVER = 'SpeckleConnector::Observers::EntitiesObserver'
MODEL_OBSERVER = 'SpeckleConnector::Observers::ModelObserver'
end
@@ -7,6 +7,7 @@ require_relative 'platform_constants'
module SpeckleConnector
dir = __dir__.dup
dir.force_encoding('UTF-8') if dir.respond_to?(:force_encoding)
HOME_PATH = (ENV['HOME']).to_s
SPECKLE_SRC_PATH = Pathname.new(File.expand_path('..', dir)).cleanpath.to_s
SPECKLE_APPDATA_PATH = case OPERATING_SYSTEM
when OS_WIN
@@ -0,0 +1,21 @@
# frozen_string_literal: true
module SpeckleConnector
COMBINE_FACES_BY_MATERIAL = :combine_faces_by_material
INCLUDE_ENTITY_ATTRIBUTES = :include_entity_attributes
INCLUDE_FACE_ENTITY_ATTRIBUTES = :include_face_entity_attributes
INCLUDE_EDGE_ENTITY_ATTRIBUTES = :include_edge_entity_attributes
INCLUDE_GROUP_ENTITY_ATTRIBUTES = :include_group_entity_attributes
INCLUDE_COMPONENT_ENTITY_ATTRIBUTES = :include_component_entity_attributes
MERGE_COPLANAR_FACES = :merge_coplanar_faces
DEFAULT_MODEL_PREFERENCES = {
COMBINE_FACES_BY_MATERIAL => true,
INCLUDE_ENTITY_ATTRIBUTES => true,
INCLUDE_FACE_ENTITY_ATTRIBUTES => true,
INCLUDE_EDGE_ENTITY_ATTRIBUTES => true,
INCLUDE_GROUP_ENTITY_ATTRIBUTES => true,
INCLUDE_COMPONENT_ENTITY_ATTRIBUTES => true,
MERGE_COPLANAR_FACES => true
}.freeze
end
@@ -2,4 +2,15 @@
module SpeckleConnector
BASE_OBJECT = 'Base'
OBJECTS_GEOMETRY_LINE = 'Objects.Geometry.Line'
OBJECTS_GEOMETRY_POLYLINE = 'Objects.Geometry.Polyline'
OBJECTS_GEOMETRY_MESH = 'Objects.Geometry.Mesh'
OBJECTS_GEOMETRY_BREP = 'Objects.Geometry.Brep'
OBJECTS_OTHER_BLOCKINSTANCE = 'Objects.Other.BlockInstance'
OBJECTS_OTHER_INSTANCE = 'Objects.Other.Instance:Objects.Other.Instance'
OBJECTS_OTHER_REVIT_REVITINSTANCE = 'Objects.Other.Revit.RevitInstance'
OBJECTS_OTHER_BLOCKDEFINITION = 'Objects.Other.BlockDefinition'
OBJECTS_OTHER_RENDERMATERIAL = 'Objects.Other.RenderMaterial'
end
@@ -5,6 +5,7 @@ require 'securerandom'
# rubocop:enable SketchupPerformance/OpenSSL
require 'digest'
require_relative 'converter'
require_relative '../speckle_entities/speckle_entity'
require_relative '../relations/many_to_one_relation'
module SpeckleConnector
@@ -15,7 +16,16 @@ module SpeckleConnector
# @return [Integer] default chunk size the determine splitting base prop into chucks
attr_reader :default_chunk_size
def initialize(default_chunk_size = 1000)
# @return [String] stream id to send conversion
attr_reader :stream_id
attr_accessor :speckle_state
# @param stream_id [String] stream id to send conversion
def initialize(speckle_state, stream_id, preferences, default_chunk_size = 1000)
@speckle_state = speckle_state
@stream_id = stream_id
@preferences = preferences
@default_chunk_size = default_chunk_size
@detach_lineage = []
@lineage = []
@@ -30,31 +40,55 @@ module SpeckleConnector
def serialize(base)
id, traversed = traverse_base(base)
@objects[id] = traversed
return id, traversed
id
end
def total_children_count(id)
@objects[id][:totalChildrenCount]
end
# @param base [Object] base object to populate all children and their relationship
# @param base_and_entities [Object] base object to populate all children and their relationship
# rubocop:disable Metrics/MethodLength
def traverse_base(base)
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/AbcSize
def traverse_base(base_and_entities)
base, entities = base_and_entities
# 1. Create random string for lineage tracking.
@lineage.append(SecureRandom.hex)
# 2. Initialize traversed base object that will be filled with traversed values or
# 2. Get last item from detach_lineage array
is_detached = @detach_lineage.pop
# unless entities.nil?
# is_sent_before = entities.all? do |entity|
# check_base_available_on_state(entity, speckle_state)
# end
# if is_sent_before
# speckle_entity = speckle_state.speckle_entities[entities.first.persistent_id]
# ref_object = detach_helper(speckle_entity.id)
# parent = @lineage[-1]
# unless @family_tree[parent].nil?
# @family_tree[parent] = @family_tree[parent].merge(speckle_entity.speckle_object[:__closure])
# end
# @objects[speckle_entity.id] = ref_object if is_detached
# return speckle_entity.id, ref_object
# end
# end
# 3. Initialize traversed base object that will be filled with traversed values or
# traversed base objects as props.
traversed_base = SpeckleObjects::Base.new(speckle_type: base[:speckle_type], id: '')
# 3.1 Remove applicationId if it is nil
traversed_base.delete(:applicationId)
# 3. Iterate all entries (key, value) of the base {Base > Hash} object
# 4. Iterate all entries (key, value) of the base {Base > Hash} object
# speckle_state = traverse_base_props(base, traversed_base)
traverse_base_props(base, traversed_base)
# this is where all props are done for current `traversed_base`
# 4. Get last item from detach_lineage array
is_detached = @detach_lineage.pop
# 5. Add closures
closure = {}
parent = @lineage.pop
@@ -82,9 +116,19 @@ module SpeckleConnector
# 10. Save object string if detached
@objects[id] = traversed_base if is_detached
if @preferences[:user][:register_speckle_entity] && !entities.nil?
entities.uniq.each do |entity|
speckle_entity = create_or_update_speckle_entity(entity, id, traversed_base)
@speckle_state = speckle_state.with_speckle_entity(speckle_entity)
end
end
return id, traversed_base
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
@@ -102,14 +146,16 @@ module SpeckleConnector
next
end
# 3.3. Determine prop is detached or not
is_prop_detach = prop[0] == '@'
# 3.3. Determine prop is dynamically detached or not
is_detach_prop = prop[0] == '@'
is_dynamically_detached = prop[0] == '@' && prop.length > 2 && prop[1] == '@'
prop = prop[2..-1] if is_dynamically_detached
# 3.4. Check prop needs to split into chunks
chunked_detach_match = prop.match(/^@\((\d*)\)/)
# 3.5. If split chunk is needed and prop value is array, then run chunking process
if value.is_a?(Array) && chunked_detach_match
if value.is_a?(Array) && !base_and_entities?(value) && chunked_detach_match
# 3.5.1. Determine chunk size, get it from prop if defined. ex: '@(31250)faces' -> 31250 = chunk size
chunk_size = chunked_detach_match[1] == '' ? default_chunk_size : chunked_detach_match[1].to_i
@@ -144,7 +190,7 @@ module SpeckleConnector
chunk_references = []
chunks.each do |chunk_element|
@detach_lineage.append(is_prop_detach)
@detach_lineage.append(is_detach_prop)
id, _traversed = traverse_base(chunk_element)
chunk_references.append(detach_helper(id))
end
@@ -152,17 +198,21 @@ module SpeckleConnector
# 3.5.7. Add chunk references to the traversed base prop without @(<chunk_size>)
traversed_base[prop.to_s.sub(chunked_detach_match[0], '')] = chunk_references
# 3.5.8. We are done chunking, good to go next
# 3.5.8. We are done chunking, good to go next property
next
end
child = traverse_value(value, is_detach_prop)
is_base = (value.is_a?(Hash) && !value[:speckle_type].nil?) ||
(base_and_entities?(value) && value[0].is_a?(Hash) && !value[0][:speckle_type].nil?)
# 3.6. traverse value according to value is a speckle object or not
if value.is_a?(Hash) && !value[:speckle_type].nil?
child = traverse_value(value, is_prop_detach)
traversed_base[prop] = is_prop_detach ? detach_helper(child[:id]) : child
else
traversed_base[prop] = traverse_value(value, is_prop_detach)
end
traversed_base[prop] = if is_base
is_detach_prop ? detach_helper(child[:id]) : child
else
child
end
end
end
# rubocop:enable Metrics/MethodLength
@@ -171,38 +221,57 @@ module SpeckleConnector
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
# Whether value has a pattern [<converted>, [<entity>, <entity>, ... <entity>]] or not.
def base_and_entities?(value)
is_array = value.is_a?(Array)
return false unless is_array
return false unless is_array && value.length == 2
value[1].all? { |v| v.is_a?(Sketchup::Entity) }
end
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Style/OptionalBooleanParameter
# rubocop:disable Metrics/AbcSize
def traverse_value(value, is_detach = false)
# 1. Return same value if value is primitive type (string, numeric, boolean)
return value unless value.is_a?(Hash) || value.is_a?(Array)
# 2. Arrays
if value.is_a?(Array)
# 2. For pure arrays (Without referencing any Sketchup Entity)
if value.is_a?(Array) && !base_and_entities?(value)
# 2.1. If it is not detached then iterate array by traversing with their value
return value.collect { |el| traverse_value(el) } unless is_detach
unless is_detach
values = value.collect do |el|
el_value = traverse_value(el)
el_value
end
return values
end
# 2.2. If it is detached than collect them into detached_list
detached_list = []
value.each do |el|
if (el.is_a?(Array) || el.is_a?(Hash)) && !el[:speckle_type].nil?
if (el.is_a?(Hash) && !el[:speckle_type].nil?) || base_and_entities?(el)
@detach_lineage.append(is_detach)
id, _traversed_base = traverse_base(el)
detached_list.append(detach_helper(id))
else
detached_list.append(traverse_value(el, is_detach))
el_value = traverse_value(el, is_detach)
detached_list.append(el_value)
end
end
return detached_list
end
# 3. Hash
return value if value[:speckle_type].nil?
return value if value.is_a?(Hash) && value[:speckle_type].nil?
# 4. Base objects
unless value[:speckle_type].nil?
if (value.is_a?(Hash) && !value[:speckle_type].nil?) || base_and_entities?(value)
@detach_lineage.append(is_detach)
_id, traversed_base = traverse_base(value)
return traversed_base
@@ -215,6 +284,7 @@ module SpeckleConnector
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Style/OptionalBooleanParameter
# rubocop:enable Metrics/AbcSize
def detach_helper(reference_id)
@lineage.each do |parent|
@@ -263,6 +333,31 @@ module SpeckleConnector
batches
end
# rubocop:enable Metrics/MethodLength
# @param entity [Sketchup::Entity] source entity object
# @param speckle_state [States::SpeckleState] the current speckle state of the {States::State}
def check_base_available_on_state(entity, speckle_state)
is_exist = speckle_state.speckle_entities.keys.include?(entity.persistent_id)
return is_exist unless is_exist
speckle_state.speckle_entities[entity.persistent_id].valid_stream_ids.include?(stream_id)
end
# Creates or updates speckle entity.
# If speckle entity exist in state, creates new one by updating old one.
# Else creates new one
# @return [SpeckleEntity] speckle entity that collects both speckle and sketchup information.
def create_or_update_speckle_entity(entity, id, traversed_base)
if speckle_state.speckle_entities.keys.include?(entity.persistent_id)
speckle_state.speckle_entities[entity.persistent_id].with_valid_stream_id(stream_id)
else
children = traversed_base[:__closure].nil? ? {} : traversed_base[:__closure]
speckle_entity = SpeckleEntities::SpeckleEntity.new(entity, id, traversed_base[:speckle_type],
children.keys, [stream_id])
speckle_entity.write_initial_base_data
speckle_entity
end
end
end
end
end
@@ -17,6 +17,11 @@ module SpeckleConnector
faces.each { |face| face.edges.each { |edge| edges << edge } }
edges.uniq!
edges.each { |edge| remove_edge_have_coplanar_faces(edge, faces, false) }
merged_faces(faces)
end
def self.merged_faces(faces)
faces.reject(&:deleted?)
end
# Detect edges to remove by checking following controls respectively;
+20 -6
View File
@@ -4,18 +4,32 @@ module SpeckleConnector
module Converters
# Helper class to convert geometries between server and Sketchup.
class Converter
# @return [States::State] the current state of the {SpeckleConnector::App}
attr_reader :state
# @return [States::SpeckleState] the current speckle state of the {States::State}
attr_reader :speckle_state
# @return [Sketchup::Model] active sketchup model.
attr_reader :sketchup_model
attr_accessor :units, :definitions, :registry, :entity_observer
# @return [String] stream id that conversion happening with it
attr_reader :stream_id
def initialize(sketchup_model)
@sketchup_model = sketchup_model
su_unit = @sketchup_model.options['UnitsOptions']['LengthUnit']
# @return [String] speckle units
attr_reader :units
attr_accessor :definitions
# @param state [States::State] the current state of the {SpeckleConnector::App}
def initialize(state, stream_id)
@state = state
@speckle_state = state.speckle_state
@sketchup_model = state.sketchup_state.sketchup_model
@stream_id = stream_id
su_unit = state.sketchup_state.length_units
@units = Converters::SKETCHUP_UNITS[su_unit]
@definitions = {}
# @registry = Sketchup.active_model.attribute_dictionary("speckle_id_registry", true)
# @entity_observer = SpeckleEntityObserver.new
end
end
end
+121 -83
View File
@@ -5,6 +5,7 @@ require_relative '../speckle_objects/other/transform'
require_relative '../speckle_objects/other/render_material'
require_relative '../speckle_objects/other/block_definition'
require_relative '../speckle_objects/other/block_instance'
require_relative '../speckle_objects/other/display_value'
require_relative '../speckle_objects/geometry/point'
require_relative '../speckle_objects/geometry/line'
require_relative '../speckle_objects/geometry/mesh'
@@ -13,6 +14,9 @@ module SpeckleConnector
module Converters
# Converts sketchup entities to speckle objects.
class ToNative < Converter
# @return [States::SpeckleState] the current speckle state of the {States::State}
attr_accessor :speckle_state
# Module aliases
GEOMETRY = SpeckleObjects::Geometry
OTHER = SpeckleObjects::Other
@@ -23,6 +27,8 @@ module SpeckleConnector
MESH = GEOMETRY::Mesh
BLOCK_DEFINITION = OTHER::BlockDefinition
BLOCK_INSTANCE = OTHER::BlockInstance
RENDER_MATERIAL = OTHER::RenderMaterial
DISPLAY_VALUE = OTHER::DisplayValue
BASE_OBJECT_PROPS = %w[applicationId id speckle_type totalChildrenCount].freeze
CONVERTABLE_SPECKLE_TYPES = %w[
@@ -31,11 +37,34 @@ module SpeckleConnector
Objects.Geometry.Mesh
Objects.Geometry.Brep
Objects.Other.BlockInstance
Objects.Other.Revit.RevitInstance
Objects.Other.BlockDefinition
Objects.Other.RenderMaterial
].freeze
def can_convert_to_native(obj)
# ReceiveObjects action call this method by giving everything that comes from server.
# Upcoming object is a referencedObject of selected commit to receive.
# UI is responsible currently to fetch objects from ObjectLoader module by calling getAndConstruct method.
# @param obj [Object] speckle commit object.
def receive_commit_object(obj)
# First create layers on the sketchup before starting traversing
# @Named Views are exception here. It does not mean a layer. But it is anti-pattern for now.
filtered_layer_containers = obj.keys.filter_map { |key| key if key.start_with?('@') && key != '@Named Views' }
create_layers(filtered_layer_containers, sketchup_model.layers)
create_views(obj.filter_map { |key, value| value if key == '@Named Views' }, sketchup_model)
# Get default commit layer from sketchup model which will be used as fallback
default_commit_layer = sketchup_model.layers.layers.find { |layer| layer.display_name == '@Untagged' }
traverse_commit_object(obj, sketchup_model.layers, default_commit_layer)
@state
end
# Conditions for converting speckle object to native sketchup entity:
# 1- `obj` is a hash
# 2- `obj` has a property as 'speckle_type'
# 3- `obj` is a convertable 'speckle_type' which sketchup supports
# @param obj [Object] candidate object to convert from speckle to sketchup.
# @return [Boolean] whether object is convertable or not.
def convertible_to_native?(obj)
return false unless obj.is_a?(Hash) && obj.key?('speckle_type')
CONVERTABLE_SPECKLE_TYPES.include?(obj['speckle_type'])
@@ -45,17 +74,6 @@ module SpeckleConnector
['Objects.BuiltElements.Revit.Parameter'].include?(obj['speckle_type'])
end
# @param obj [Object] speckle commit object.
def receive_commit_object(obj, model_preferences)
# First create layers on the sketchup before starting traversing
filtered_layer_containers = obj.keys.filter_map { |key| key if key.start_with?('@') && key != '@Named Views' }
create_layers(filtered_layer_containers, sketchup_model.layers)
create_views(obj.filter_map { |key, value| value if key == '@Named Views' }, sketchup_model)
# Define default commit layer which is the fallback
default_commit_layer = sketchup_model.layers.layers.find { |layer| layer.display_name == '@Untagged' }
traverse_commit_object(obj, sketchup_model.layers, default_commit_layer, model_preferences)
end
# Create actual Sketchup layers from layer_paths that taken from Speckle base object.
# @param layer_paths [Array<String>] layer paths to decompose it to folders and it's layers.
# @param folder [Sketchup::Layers, Sketchup::LayerFolder] folder to create folders and layers under it.
@@ -74,6 +92,7 @@ module SpeckleConnector
# @param views [Array] views.
# @param sketchup_model [Sketchup::Model] active sketchup model.
# rubocop:disable Metrics/AbcSize
def create_views(views, sketchup_model)
return if views.empty?
@@ -86,6 +105,30 @@ module SpeckleConnector
my_camera = Sketchup::Camera.new(origin, target, [0, 0, 1], !view['isOrthogonal'], view['lens'])
sketchup_model.active_view.camera = my_camera
sketchup_model.pages.add(view['name'])
page = sketchup_model.pages[view['name']]
set_page_update_properties(page, view['update_properties'])
set_rendering_options(page.rendering_options, view['rendering_options'])
end
end
# rubocop:enable Metrics/AbcSize
# @param page [Sketchup::Page] scene to update -update properties-
def set_page_update_properties(page, update_properties)
update_properties.each do |prop, value|
page.instance_variable_set(:"@#{prop}", value)
end
end
# @param rendering_options [Sketchup::RenderingOptions] rendering options of scene (page)
def set_rendering_options(rendering_options, speckle_rendering_options)
speckle_rendering_options.each do |prop, value|
next if rendering_options[prop].nil?
rendering_options[prop] = if value.is_a?(Hash)
SpeckleObjects::Others::Color.to_native(value)
else
value
end
end
end
@@ -126,9 +169,9 @@ module SpeckleConnector
# self-caller method means that call itself according to conditions inside of it.
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def traverse_commit_object(obj, commit_folder, layer, model_preferences)
if can_convert_to_native(obj)
convert_to_native(obj, layer, model_preferences)
def traverse_commit_object(obj, commit_folder, layer)
if convertible_to_native?(obj)
@state = convert_to_native(@state, obj, layer)
elsif obj.is_a?(Hash) && obj.key?('speckle_type')
return if ignored_speckle_type?(obj)
@@ -138,16 +181,16 @@ module SpeckleConnector
props.each do |prop|
layer_path = prop if prop.start_with?('@') && obj[prop].is_a?(Array)
layer = find_layer(layer_path, commit_folder, layer)
traverse_commit_object(obj[prop], commit_folder, layer, model_preferences)
traverse_commit_object(obj[prop], commit_folder, layer)
end
else
# puts(">>> Found #{obj['speckle_type']}: #{obj['id']} with displayValue.")
convert_to_native(obj, layer, model_preferences)
@state = convert_to_native(@state, obj, layer)
end
elsif obj.is_a?(Hash)
obj.each_value { |value| traverse_commit_object(value, commit_folder, layer, model_preferences) }
obj.each_value { |value| traverse_commit_object(value, commit_folder, layer) }
elsif obj.is_a?(Array)
obj.each { |value| traverse_commit_object(value, commit_folder, layer, model_preferences) }
obj.each { |value| traverse_commit_object(value, commit_folder, layer) }
end
end
# rubocop:enable Metrics/CyclomaticComplexity
@@ -185,81 +228,76 @@ module SpeckleConnector
end
end
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/MethodLength
def convert_to_native(obj, layer, model_preferences, entities = sketchup_model.entities)
convert = method(:convert_to_native)
unless obj['displayValue'].nil?
return display_value_to_native_component(obj, layer, entities, model_preferences, &convert)
end
def speckle_object_to_native(obj)
return DISPLAY_VALUE.method(:to_native) unless obj['displayValue'].nil?
case obj['speckle_type']
when 'Objects.Geometry.Line', 'Objects.Geometry.Polyline' then LINE.to_native(obj, layer, entities)
when 'Objects.Other.BlockInstance' then BLOCK_INSTANCE.to_native(sketchup_model, obj, layer, entities,
model_preferences, &convert)
when 'Objects.Other.BlockDefinition' then BLOCK_DEFINITION.to_native(sketchup_model, obj, layer,
obj['name'],
obj['always_face_camera'],
model_preferences,
obj['sketchup_attributes'],
obj['applicationId'],
&convert)
when 'Objects.Geometry.Mesh' then MESH.to_native(sketchup_model, obj, layer, entities, model_preferences)
when 'Objects.Geometry.Brep' then MESH.to_native(sketchup_model, obj['displayValue'], layer, entities,
model_preferences)
end
SPECKLE_OBJECT_TO_NATIVE[obj['speckle_type']]
end
SPECKLE_OBJECT_TO_NATIVE = {
OBJECTS_GEOMETRY_LINE => LINE.method(:to_native),
OBJECTS_GEOMETRY_POLYLINE => LINE.method(:to_native),
OBJECTS_GEOMETRY_MESH => MESH.method(:to_native),
OBJECTS_GEOMETRY_BREP => MESH.method(:to_native),
OBJECTS_OTHER_BLOCKDEFINITION => BLOCK_DEFINITION.method(:to_native),
OBJECTS_OTHER_BLOCKINSTANCE => BLOCK_INSTANCE.method(:to_native),
OBJECTS_OTHER_REVIT_REVITINSTANCE => BLOCK_INSTANCE.method(:to_native),
OBJECTS_OTHER_RENDERMATERIAL => RENDER_MATERIAL.method(:to_native)
}.freeze
# @param state [States::State] state of the speckle application
def convert_to_native(state, obj, layer, entities = sketchup_model.entities)
# store this method as parameter to re-call it inner callstack
convert_to_native = method(:convert_to_native)
# Get 'to_native' method to convert upcoming speckle object to native sketchup entity
to_native_method = speckle_object_to_native(obj)
# Call 'to_native' method by passing this method itself to handle nested 'to_native' conversions.
# It returns updated state and converted entities.
state, converted_entities = to_native_method.call(state, obj, layer, entities, &convert_to_native)
# Create levels as section planes if they exists
create_levels(state, obj)
# Create speckle entities from sketchup entities to achieve continuous traversal.
convert_to_speckle_entities(state, obj, converted_entities)
rescue StandardError => e
puts("Failed to convert #{obj['speckle_type']} (id: #{obj['id']})")
puts(e)
nil
return state
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/MethodLength
# Creates a component definition and instance from a speckle object with a display value
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/MethodLength
def display_value_to_native_component(obj, layer, entities, model_preferences, &convert)
obj_id = obj['applicationId'].to_s.empty? ? obj['id'] : obj['applicationId']
# @param state [States::State] state of the speckle application
def create_levels(state, speckle_object)
return state if speckle_object['level'].nil?
return state unless speckle_object['level']['speckle_type'].include?('Objects.BuiltElements.Level')
block_definition = obj['@blockDefinition'] || obj['blockDefinition']
level_name = speckle_object['level']['name'] || speckle_object['level']['id']
entities = state.sketchup_state.sketchup_model.entities
is_exist = entities.grep(Sketchup::SectionPlane).any? { |sp| sp.name == level_name }
return state if is_exist
definition = BLOCK_DEFINITION.to_native(
sketchup_model,
obj['displayValue'],
layer,
"def::#{obj_id}",
if block_definition.nil?
false
else
block_definition['always_face_camera'].nil? ? false : block_definition['always_face_camera']
end,
model_preferences,
if block_definition.nil?
nil
else
block_definition['sketchup_attributes'].nil? ? nil : block_definition['sketchup_attributes']
end,
obj_id,
&convert
)
elevation = SpeckleObjects::Geometry.length_to_native(speckle_object['level']['elevation'],
speckle_object['level']['units'])
find_and_erase_existing_instance(definition, obj_id)
t_arr = obj['transform']
transform = t_arr.nil? ? Geom::Transformation.new : OTHER::Transform.to_native(t_arr, units)
instance = entities.add_instance(definition, transform)
instance.name = obj_id
instance
shift_value = SpeckleObjects::Geometry.length_to_native(1.5, 'm')
section_plane = entities.add_section_plane([0, 0, elevation + shift_value], [0, 0, -1])
section_plane.name = level_name
state
end
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/MethodLength
# Takes a component definition and finds and erases the first instance with the matching name
# (and optionally the applicationId)
def find_and_erase_existing_instance(definition, name, app_id = '')
definition.instances.find { |ins| ins.name == name || ins.guid == app_id }&.erase!
# @param state [States::State] state of the application
def convert_to_speckle_entities(state, speckle_object, entities)
return state unless state.user_state.user_preferences[:register_speckle_entity]
speckle_id = speckle_object['id']
speckle_type = speckle_object['speckle_type']
children = speckle_object['__closure'].nil? ? [] : speckle_object['__closure']
speckle_state = state.speckle_state
entities.each do |entity|
ent = SpeckleEntities::SpeckleEntity.new(entity, speckle_id, speckle_type, children, [stream_id])
ent.write_initial_base_data
speckle_state = speckle_state.with_speckle_entity(ent)
end
state.with_speckle_state(speckle_state)
end
end
end
+106 -55
View File
@@ -8,110 +8,132 @@ require_relative '../speckle_objects/geometry/length'
require_relative '../speckle_objects/geometry/mesh'
require_relative '../speckle_objects/other/block_instance'
require_relative '../speckle_objects/other/block_definition'
require_relative '../speckle_objects/other/rendering_options'
require_relative '../speckle_objects/built_elements/view3d'
require_relative '../constants/path_constants'
module SpeckleConnector
module Converters
# Converts sketchup entities to speckle objects.
class ToSpeckle < Converter
# @return [Hash{Symbol=>Array}] layers to hold it's objects under the base object.
attr_reader :layers
def initialize(sketchup_model)
super(sketchup_model)
@layers = add_all_layers
end
# Convert selected objects by putting them into related array that grouped by layer.
# @return [Hash{Symbol=>Array}] layers -which only have objects- to hold it's objects under the base object.
def convert_selection_to_base(preferences)
layers = add_all_layers
state = speckle_state
sketchup_model.selection.each do |entity|
converted_object = convert(entity, preferences)
new_speckle_state, converted_object_with_entity = convert(entity, preferences, state)
state = new_speckle_state
layer_name = entity_layer_path(entity)
layers[layer_name].push(converted_object)
layers[layer_name].push(converted_object_with_entity)
end
# send only layers that have any object
# send only+ layers that have any object
base_object_properties = layers.reject { |_layer_name, objects| objects.empty? }
add_views(base_object_properties) if sketchup_model.pages.any?
SpeckleObjects::Base.with_detached_layers(base_object_properties)
return state, SpeckleObjects::Base.with_detached_layers(base_object_properties)
end
# Add views from pages.
# @param base_object_properties [Hash] dynamically attached base object properties.
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
def add_views(base_object_properties)
views = []
sketchup_model.pages.each do |page|
cam = page.camera
origin = SpeckleObjects::Geometry::Point.new(
SpeckleObjects::Geometry.length_to_speckle(cam.eye[0], @units),
SpeckleObjects::Geometry.length_to_speckle(cam.eye[1], @units),
SpeckleObjects::Geometry.length_to_speckle(cam.eye[2], @units),
@units
)
target = SpeckleObjects::Geometry::Point.new(
SpeckleObjects::Geometry.length_to_speckle(cam.target[0], @units),
SpeckleObjects::Geometry.length_to_speckle(cam.target[1], @units),
SpeckleObjects::Geometry.length_to_speckle(cam.target[2], @units),
@units
)
direction = SpeckleObjects::Geometry::Vector.new(
SpeckleObjects::Geometry.length_to_speckle(cam.direction[0], @units),
SpeckleObjects::Geometry.length_to_speckle(cam.direction[1], @units),
SpeckleObjects::Geometry.length_to_speckle(cam.direction[2], @units),
@units
)
origin = get_camera_origin(cam)
target = get_camera_target(cam)
direction = get_camera_direction(cam)
update_properties = get_scene_update_properties(page)
rendering_options = SpeckleObjects::Others::RenderingOptions.to_speckle(page.rendering_options)
view = SpeckleObjects::BuiltElements::View3d.new(
page.name,
origin, target, direction, SpeckleObjects::Geometry::Vector.new(0, 0, 1, @units),
cam.perspective?, cam.fov, @units, page.name
page.name, origin, target, direction, SpeckleObjects::Geometry::Vector.new(0, 0, 1, @units),
cam.perspective?, cam.fov, @units, page.name, update_properties, rendering_options
)
views.append(view)
end
base_object_properties['@Named Views'] = views
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
# Get scene properties
# @param page [Sketchup::Page] page on sketchup.
def get_scene_update_properties(page)
{
use_axes: page.use_axes?,
use_camera: page.use_camera?,
use_hidden_geometry: page.use_hidden_geometry?,
use_hidden_layers: page.use_hidden_layers?,
use_hidden_objects: page.use_hidden_objects?,
use_rendering_options: page.use_rendering_options?,
use_section_planes: page.use_section_planes?,
use_shadow_info: page.use_shadow_info?,
use_style: page.use_style?
}
end
# Serialized and traversed information to send batches.
# @param base [SpeckleObjects::Base] base object to serialize.
# @param base_and_entity [SpeckleObjects::Base] base object to serialize.
# @return [String, Integer, Array<Object>] base id, total_children_count of base and batches
def send_info(base)
serializer = SpeckleConnector::Converters::BaseObjectSerializer.new
# t = Time.now.to_f
id, _traversed = serializer.serialize(base)
# puts "Generating traversed object elapsed #{Time.now.to_f - t} s"
def serialize(base_and_entity, speckle_state, preferences)
serializer = SpeckleConnector::Converters::BaseObjectSerializer.new(speckle_state, stream_id, preferences)
t = Time.now.to_f
id = serializer.serialize(base_and_entity)
batches = serializer.batch_objects
# write_to_speckle_folder(id, batches)
puts "Generating traversed object elapsed #{Time.now.to_f - t} s"
base_total_children_count = serializer.total_children_count(id)
return id, base_total_children_count, serializer.batch_objects
return id, base_total_children_count, batches, serializer.speckle_state
end
def write_to_speckle_folder(id, batches)
folder_path = "#{HOME_PATH}/Speckle"
file_path = "#{folder_path}/#{id}.json"
FileUtils.mkdir_p(folder_path) unless File.exist?(folder_path)
File.write(file_path, batches.first)
end
# @param entity [Sketchup::Entity] sketchup entity to convert Speckle.
def convert(entity, preferences)
# @param speckle_state [States::SpeckleState] the current speckle state of the {States::State}
# @param parent [Symbol, String] parent of the Sketchup Entity to be converted.
# rubocop:disable Metrics/MethodLength
def convert(entity, preferences, speckle_state, parent = :base)
convert = method(:convert)
if entity.is_a?(Sketchup::Edge)
return SpeckleObjects::Geometry::Line.from_edge(entity, @units, preferences[:model]).to_h
line = SpeckleObjects::Geometry::Line.from_edge(entity, @units, preferences[:model]).to_h
return speckle_state, [line, [entity]]
end
if entity.is_a?(Sketchup::Face)
return SpeckleObjects::Geometry::Mesh.from_face(entity, @units, preferences[:model])
mesh = SpeckleObjects::Geometry::Mesh.from_face(entity, @units, preferences[:model])
return speckle_state, [mesh, [entity]]
end
if entity.is_a?(Sketchup::Group)
return SpeckleObjects::Other::BlockInstance.from_group(entity, @units, @definitions, preferences, &convert)
new_speckle_state, block_instance = SpeckleObjects::Other::BlockInstance.from_group(
entity, @units, preferences, speckle_state, &convert
)
speckle_state = new_speckle_state
return speckle_state, [block_instance, [entity]]
end
if entity.is_a?(Sketchup::ComponentInstance)
return SpeckleObjects::Other::BlockInstance.from_component_instance(entity, @units, @definitions,
preferences, &convert)
end
if entity.is_a?(Sketchup::ComponentDefinition)
return SpeckleObjects::Other::BlockDefinition.from_definition(entity, @units, @definitions, preferences,
&convert)
new_speckle_state, block_instance = SpeckleObjects::Other::BlockInstance.from_component_instance(
entity, @units, preferences, speckle_state, &convert
)
speckle_state = new_speckle_state
return speckle_state, [block_instance, [entity]]
end
nil
if entity.is_a?(Sketchup::ComponentDefinition)
new_speckle_state, block_definition = SpeckleObjects::Other::BlockDefinition.from_definition(
entity, @units, @definitions, preferences, speckle_state, parent, &convert
)
speckle_state = new_speckle_state
return speckle_state, [block_definition, [entity]]
end
return speckle_state, nil
end
# rubocop:enable Metrics/MethodLength
# Create layers -> {Hash{Symbol=>Array}} from sketchup model with empty array as hash entry values.
# This method add first headless layers (not belong to any folder),
@@ -174,6 +196,35 @@ module SpeckleConnector
folder_name(folder.folder, folders.push(folder.display_name))
end
end
private
def get_camera_direction(cam)
SpeckleObjects::Geometry::Vector.new(
SpeckleObjects::Geometry.length_to_speckle(cam.direction[0], @units),
SpeckleObjects::Geometry.length_to_speckle(cam.direction[1], @units),
SpeckleObjects::Geometry.length_to_speckle(cam.direction[2], @units),
@units
)
end
def get_camera_target(cam)
SpeckleObjects::Geometry::Point.new(
SpeckleObjects::Geometry.length_to_speckle(cam.target[0], @units),
SpeckleObjects::Geometry.length_to_speckle(cam.target[1], @units),
SpeckleObjects::Geometry.length_to_speckle(cam.target[2], @units),
@units
)
end
def get_camera_origin(camera)
SpeckleObjects::Geometry::Point.new(
SpeckleObjects::Geometry.length_to_speckle(camera.eye[0], @units),
SpeckleObjects::Geometry.length_to_speckle(camera.eye[1], @units),
SpeckleObjects::Geometry.length_to_speckle(camera.eye[2], @units),
@units
)
end
end
end
end
@@ -0,0 +1,33 @@
# frozen_string_literal: true
require_relative 'event_observer'
module SpeckleConnector
module Observers
# App observer.
class AppObserver < Sketchup::AppObserver
include EventObserver
# rubocop:disable Naming/MethodName
# SketchUp observer method triggered when new empty model is created.
#
# @param model (Sketchup::Model): The active model object.
def onNewModel(model)
push_event(:onNewModel, model)
end
# SketchUp observer method triggered when previously saved model is opened.
#
# @param model (Sketchup::Model) - The active model object.
def onOpenModel(model)
push_event(:onOpenModel, model)
end
# SketchUp observer method triggered when user exists SketchUp.
def onQuit
push_event(:onQuit)
end
# rubocop:enable Naming/MethodName
end
end
end
@@ -1,26 +1,30 @@
# frozen_string_literal: true
require_relative 'event_observer'
module SpeckleConnector
module Observers
# Entities observer.
class EntitiesObserver < Sketchup::EntitiesObserver
attr_accessor :registry
def initialize
super()
@registry = Sketchup.active_model.attribute_dictionary('speckle_id_registry', true)
end
include EventObserver
# rubocop:disable Naming/MethodName
def onEraseEntity(entity)
app_id = entity.get_attribute('speckle', 'applicationId')
return if app_id.nil?
# @param entities (Sketchup::Entities)
# @param entity (Sketchup::Entity)
def onElementAdded(entities, entity)
push_event(:onElementAdded, entities, entity)
end
p(app_id)
# @param entities (Sketchup::Entities)
# @param entity (Sketchup::Entity)
def onElementModified(entities, entity)
push_event(:onElementModified, entities, entity)
end
@registry.delete_key(app_id)
p(@registry)
# @param entities (Sketchup::Entities)
# @param entity_id (Integer) id of the removed entity.
def onElementRemoved(entities, entity_id)
push_event(:onElementRemoved, entities, entity_id)
end
# rubocop:enable Naming/MethodName
end
@@ -0,0 +1,26 @@
# frozen_string_literal: true
require_relative 'event_hash'
require_relative '../actions/on_events_action'
module SpeckleConnector
module Observers
# Event handler class.
class EventHandler
# @return [SpeckleConnectorApp] an object that contains current state of speckle objects
attr_reader :app
def initialize(app)
@app = app
end
def handle_events(events)
run_handlers(events)
end
def run_handlers(events)
app.update_state!(Actions::OnEventsAction, events)
end
end
end
end
@@ -0,0 +1,20 @@
# frozen_string_literal: true
require_relative '../ext/immutable_ruby/core'
module SpeckleConnector
module Observers
# Collection of events.
class EventHash < Immutable::Hash
def push_event(observer_class, event_name, data)
observer_events = self[observer_class] || Immutable::EmptyHash
name_events = observer_events[event_name] || Immutable::EmptyVector
name_events = name_events.add(data)
observer_events = observer_events.put(event_name, name_events)
put(observer_class, observer_events)
end
end
EMPTY_EVENT_HASH = EventHash.new([])
end
end
@@ -0,0 +1,43 @@
# frozen_string_literal: true
require_relative 'event_hash'
require_relative '../actions/on_events_action'
module SpeckleConnector
module Observers
# Observer classes includes it to check common operations for all observer classes.
module EventObserver
# @return [ObserverHandler] handler for observer events
attr_reader :observer_handler
def initialize(observer_handler)
super()
@observer_handler = observer_handler
end
def push_event(event_name, *event_data)
return if observers_disabled?
@observer_handler.handle_event!(self.class.name, event_name, event_data)
end
def observers_disabled?
@observer_handler.observers_disabled?
end
# Push event only once. If the event is already registered, don't push it again
# @param event_name [Symbol] the name of the event
# @param event_data [Array] the optional data that comes with the event
def push_once(event_name, *event_data)
return if observers_disabled?
# Don't push anything if the selection event was already registered
class_events = @observer_handler.events[self.class]
events = class_events && class_events[event_name]
return if events&.any?
push_event(event_name, *event_data)
end
end
end
end
@@ -0,0 +1,30 @@
# frozen_string_literal: true
require_relative 'app_observer'
require_relative 'entities_observer'
require_relative 'observer_handler'
require_relative 'model_observer'
require_relative 'event_handler'
require_relative '../constants/observer_constants'
module SpeckleConnector
module Observers
# Factory class to create observers and it's handler
module Factory
module_function
def create_handler(app)
event_handler = EventHandler.new(app)
ObserverHandler.new(event_handler)
end
def create_observers(handler)
{
APP_OBSERVER => AppObserver.new(handler),
ENTITIES_OBSERVER => EntitiesObserver.new(handler),
MODEL_OBSERVER => ModelObserver.new(handler)
}.freeze
end
end
end
end
@@ -0,0 +1,19 @@
# frozen_string_literal: true
require_relative 'event_observer'
module SpeckleConnector
module Observers
# Model related event observers.
class ModelObserver < Sketchup::ModelObserver
include EventObserver
# rubocop:disable Naming/MethodName
# @param model (Sketchup::Model)
def onActivePathChanged(model)
push_event(:onActivePathChanged, model)
end
# rubocop:enable Naming/MethodName
end
end
end
@@ -0,0 +1,68 @@
# frozen_string_literal: true
require_relative 'event_hash'
module SpeckleConnector
module Observers
# Class to handle observer events.
class ObserverHandler
# @return [EventHash] registered events
attr_reader :events
# @return [Float] time when first observer was added
attr_reader :start_time
def initialize(event_handler)
@event_handler = event_handler
clear_events!
@observers_disabled = false
@observers_in_progress = false
@timer_in_progress = false
end
def handle_event!(observer_class, event_name, data)
puts "## Register #{observer_class} event #{event_name} ##"
puts " data = #{data.inspect}"
return if observers_disabled?
@events = @events.push_event(observer_class, event_name, data)
finish
end
def finish
return if @observers_in_progress
@observers_in_progress = true
@start_time = Time.now.to_f
UI.start_timer(0, false) { finish_in_timer }
end
def finish_in_timer
return if @timer_in_progress
@timer_in_progress = true
@event_handler.handle_events(events)
ensure
clear_events!
@observers_in_progress = false
@timer_in_progress = false
end
def observers_disabled?
@observers_disabled
end
def with_observers_disabled(&block)
previous_state = @observers_disabled
@observers_disabled = true
block.call
ensure
@observers_disabled = previous_state
end
def clear_events!
@events = EMPTY_EVENT_HASH
end
end
end
end
@@ -3,6 +3,7 @@
require_relative '../ext/sqlite3'
require_relative '../immutable/immutable'
require_relative '../constants/path_constants'
require_relative '../constants/pref_constants'
require_relative '../sketchup_model/dictionary/speckle_model_dictionary_handler'
module SpeckleConnector
@@ -10,66 +11,88 @@ module SpeckleConnector
module Preferences
include Immutable::ImmutableUtils
DICT_HANDLER = SketchupModel::Dictionary::SpeckleModelDictionaryHandler
DEFAULT_PREFERENCES = "('configSketchup', '{\"DarkTheme\":false}');"
# rubocop:disable Layout/LineLength
DEFAULT_CONFIG = "('configSketchup', '{\"dark_theme\":false, \"diffing\":false, \"register_speckle_entity\":false}');"
# rubocop:enable Layout/LineLength
DEFAULT_PREFERENCES = '{"dark_theme":false, "diffing":false, "register_speckle_entity": false}'
# @param sketchup_model [Sketchup::Model] active model.
# rubocop:disable Metrics/MethodLength
def self.init_preferences(sketchup_model)
# Init sqlite database
def self.read_preferences(sketchup_model)
db = Sqlite3::Database.new(SPECKLE_CONFIG_DB_PATH)
user_preferences = validate_user_preferences(db)
model_preferences = validate_model_preferences(sketchup_model)
Immutable::Hash.new(
{
user: user_preferences,
model: model_preferences
}
)
end
# Check configSketchup key is valid or not, otherwise init with default settings
if db.exec("SELECT content FROM 'objects' WHERE hash = 'configSketchup'").empty?
db.exec("INSERT INTO 'objects' VALUES #{DEFAULT_PREFERENCES}")
# Whether row data is complete with preference or not.
# It is useful for backward compatibility, when we add new preferences it should be reset when user initialize it.
def self.data_complete?(row_data)
return false if row_data.empty?
data = JSON.parse(row_data.first.first)
return false if data['dark_theme'].nil? || data['diffing'].nil? || data['register_speckle_entity'].nil?
true
end
# Validates current preferences. If there are incomplete data then this method resets it with default preferences.
# @param database [Sqlite3::Database] database for queries.
# rubocop:disable Metrics/MethodLength
def self.validate_user_preferences(database)
row_data = database.exec("SELECT content FROM 'objects' WHERE hash = 'configSketchup'")
is_config_sketchup_exist = !row_data.empty?
is_data_complete = data_complete?(row_data)
if !is_config_sketchup_exist || !is_data_complete
if is_config_sketchup_exist
unless is_data_complete
# Update table with default preferences
database.exec("UPDATE 'objects' SET content = '#{DEFAULT_PREFERENCES}' WHERE hash = 'configSketchup'")
end
else
# Insert configSketchup completely to objects.
database.exec("INSERT INTO 'objects' VALUES #{DEFAULT_CONFIG}")
end
end
# Select data
data = db.exec("SELECT content FROM 'objects' WHERE hash = 'configSketchup'").first.first
row_data = database.exec("SELECT content FROM 'objects' WHERE hash = 'configSketchup'").first.first
# Parse string to hash
data_hash = JSON.parse(data).to_h
data_hash = JSON.parse(row_data).to_h
# Get current theme value
dark_theme = data_hash['DarkTheme']
dark_theme = data_hash['dark_theme']
diffing = data_hash['diffing']
register_speckle_entity = data_hash['register_speckle_entity']
speckle_dictionary = sketchup_model.attribute_dictionary('Speckle')
if speckle_dictionary
Immutable::Hash.new(
{
user: {
dark_theme: dark_theme
},
model: {
combine_faces_by_material: DICT_HANDLER.get_attribute(sketchup_model,
:combine_faces_by_material, 'Speckle'),
include_entity_attributes: DICT_HANDLER.get_attribute(sketchup_model,
:include_entity_attributes, 'Speckle'),
merge_coplanar_faces: DICT_HANDLER.get_attribute(sketchup_model,
:merge_coplanar_faces, 'Speckle')
}
}
)
else
DICT_HANDLER.write_initial_model_data(sketchup_model, default_model_preferences)
Immutable::Hash.new(
{
user: {
dark_theme: dark_theme
},
model: default_model_preferences
}
)
end
{
dark_theme: dark_theme,
diffing: diffing,
register_speckle_entity: register_speckle_entity
}.freeze
end
# rubocop:enable Metrics/MethodLength
def self.default_model_preferences
{
combine_faces_by_material: true,
include_entity_attributes: true,
merge_coplanar_faces: true
}
# @param sketchup_model [Sketchup::Model] sketchup model to validate model preferences
def self.validate_model_preferences(sketchup_model)
speckle_dictionary = sketchup_model.attribute_dictionary('Speckle')
if speckle_dictionary.nil?
DICT_HANDLER.write_initial_model_data(sketchup_model, DEFAULT_MODEL_PREFERENCES)
return DEFAULT_MODEL_PREFERENCES
end
DEFAULT_MODEL_PREFERENCES.collect do |pref_key, default_value|
pref_value = DICT_HANDLER.get_attribute(
sketchup_model,
pref_key,
'Speckle'
)
DICT_HANDLER.set_attribute(sketchup_model, pref_key, default_value, 'Speckle') if pref_value.nil?
pref_value.nil? ? [pref_key, default_value] : [pref_key, pref_value]
end.to_h
end
end
end
@@ -8,6 +8,8 @@ module SpeckleConnector
# `A has child B` is Many to One relationship. Each person can have only one parent,
# but one parent can have several children.
class ManyToOneRelation
attr_reader :parent_table, :children_table
# @param child [Object] the child element in the relation to look for parent
# @return [Object] the parent element that is in relation with the child
# @return [nil] if the child element is not in the relation with any elements
@@ -0,0 +1,47 @@
# frozen_string_literal: true
require_relative '../../constants/dict_constants'
module SpeckleConnector
# Operations related to {SketchupModel}.
module SketchupModel
# Definitions to store for entities.
class Definitions
def self.from_sketchup_model(model)
definitions = model.definitions
definitions = definitions.select do |definition|
!definition.attribute_dictionaries.nil? &&
definition.attribute_dictionaries.any? { |dict| dict.name == SPECKLE_BASE_OBJECT }
end
Definitions.new(definitions)
end
def add_definition(definition)
old_definition = @definitions_by_guid[definition.guid]
return self if definition == old_definition
new_definitions = @definitions.append(definition)
Definitions.new(new_definitions)
end
def initialize(definitions = [])
@definitions = definitions
@definitions_by_name = definitions.collect do |definition|
[definition.name, definition]
end.to_h.freeze
@definitions_by_guid = definitions.collect do |definition|
[definition.guid, definition]
end.to_h.freeze
freeze
end
def by_guid(guid)
@definitions_by_guid[guid]
end
def by_name(name)
@definitions_by_name[name]
end
end
end
end
@@ -9,6 +9,12 @@ module SpeckleConnector
class DictionaryHandler
DICTIONARY_NAME = 'Speckle_Base_Object'
IGNORED_DICTIONARY_NAMES = [
DICTIONARY_NAME,
'IFC 4',
'IFC 2x3'
].freeze
# @param entity [Sketchup::Entity] entity to get attribute dictionaries
def self.attribute_dictionaries_to_speckle(entity)
dictionaries = {}
@@ -16,18 +22,23 @@ module SpeckleConnector
entity.attribute_dictionaries.each do |att_dict|
dict_name = att_dict == '' ? 'empty_dictionary_name' : att_dict.name
dictionaries[dict_name] = att_dict.to_h.to_json unless att_dict.name == 'Speckle_Base_Object'
dictionaries[dict_name] = att_dict.to_h unless IGNORED_DICTIONARY_NAMES.include?(att_dict.name)
end
dictionaries
end
# @param entity [Sketchup::Entity] entity to set attribute dictionaries
# rubocop:disable Metrics/CyclomaticComplexity
def self.attribute_dictionaries_to_native(entity, dictionaries)
return if dictionaries.nil?
classification_to_native(entity, dictionaries) if entity.is_a?(Sketchup::ComponentDefinition)
dictionaries.each do |dict_name, entries|
next unless entries.is_a?(Hash)
dict_name = dict_name == 'empty_dictionary_name' ? '' : dict_name
JSON.parse(entries).each do |key, value|
entries.each do |key, value|
set_attribute(entity, key, value, dict_name)
rescue StandardError => e
puts("Failed to write key: #{key} value: #{value} to dictionary #{dict_name}")
@@ -35,6 +46,19 @@ module SpeckleConnector
end
end
end
# rubocop:enable Metrics/CyclomaticComplexity
# Classification is ComponentDefinition specific, so they can be added only definition by add_classification
# method.
# @param definition_entity [Sketchup::ComponentDefinition] definition to add callback
def self.classification_to_native(definition_entity, dictionaries)
applied_schema_types = dictionaries['AppliedSchemaTypes']
return if applied_schema_types.nil?
applied_schema_types.each do |key, value|
definition_entity.add_classification(key, value)
end
end
# @param entity [Sketchup::Entity] the sketchup entity of Speckle object
# @param key [Symbol] the name of the attribute
@@ -11,16 +11,21 @@ module SpeckleConnector
class SpeckleEntityDictionaryHandler < DictionaryHandler
# Writes initial data while speckle entity is creating first time.
# @param sketchup_entity [Sketchup::Entity] Sketchup entity to write data into it's attribute dictionary.
def self.write_initial_base_data(sketchup_entity)
# rubocop:disable Metrics/ParameterLists
def self.write_initial_base_data(sketchup_entity, application_id, id, speckle_type, children, stream_id)
initial_dict_data = {
# Add here more if you want to write here initial data
SPECKLE_ID => '',
SPECKLE_TYPE => BASE_OBJECT,
APPLICATION_ID => '',
TOTAL_CHILDREN_COUNT => 0
SPECKLE_ID => id,
APPLICATION_ID => application_id,
SPECKLE_TYPE => speckle_type,
TOTAL_CHILDREN_COUNT => children.length,
CHILDREN => children,
VALID_STREAM_IDS => [stream_id],
INVALID_STREAM_IDS => []
}
set_hash(sketchup_entity, initial_dict_data)
end
# rubocop:enable Metrics/ParameterLists
end
end
end
@@ -0,0 +1,55 @@
# frozen_string_literal: true
require_relative '../../constants/mat_constants'
module SpeckleConnector
# Operations related to {SketchupModel}.
module SketchupModel
# Materials to store for entities.
class Materials
def self.from_sketchup_model(model)
materials = model.materials
materials = materials.select do |material|
!material.attribute_dictionaries.nil? &&
material.attribute_dictionaries.any? { |dict| dict.name == MAT_DICTIONARY }
end
mat_hash = materials.collect { |material| [material.get_attribute(MAT_DICTIONARY, MAT_ID), material] }.to_h
Materials.new(mat_hash)
end
def initialize(material_hash = {})
@materials_by_id = material_hash.freeze
@id_by_materials = material_hash.collect { |id, material| [material, id] }.to_h.freeze
freeze
end
def add_material(id, material)
old_material = @materials_by_id[id.to_sym]
return self if material == old_material
new_material_hash = @materials_by_id.merge({ id.to_sym => material })
Materials.new(new_material_hash)
end
def by_id(id)
@materials_by_id[id.to_sym]
end
def material_id(material)
@id_by_materials[material]
end
def add_speckle_material
@materials_by_id[MAT_ADD]
end
def edit_speckle_material
@materials_by_id[MAT_EDIT]
end
def remove_speckle_material
@materials_by_id[MAT_REMOVE]
end
end
end
end
@@ -0,0 +1,51 @@
# frozen_string_literal: true
require_relative '../../speckle_entities/speckle_entity'
require_relative '../../constants/dict_constants'
module SpeckleConnector
# Operations related to {SketchupModel}.
module SketchupModel
# Reader model for sketchup model.
module Reader
# Reader module for speckle entities.
module SpeckleEntitiesReader
# @param entities [Sketchup::Entities] entities to collect speckle entities.
def self.read(entities)
speckle_entities = {}
entities.each do |entity|
speckle_entities[entity.persistent_id] = read_speckle_entity(entity) if speckle_entity?(entity)
next unless entity.is_a?(Sketchup::Group) || entity.is_a?(Sketchup::ComponentInstance)
if speckle_entity?(entity.definition)
speckle_entities[entity.definition.persistent_id] = read_speckle_entity(entity.definition)
end
definition_speckle_entities = read(entity.definition.entities)
speckle_entities = speckle_entities.merge(definition_speckle_entities)
end
speckle_entities
end
# @param entity [Sketchup::Entity] sketchup entity to read from attribute dictionary.
def self.read_speckle_entity(entity)
dict = entity.attribute_dictionaries.to_a.find { |d| d.name == SPECKLE_BASE_OBJECT }
speckle_id = dict[:speckle_id]
speckle_type = dict[:speckle_type]
children = dict[:children]
valid_stream_ids = dict[:valid_stream_ids]
invalid_stream_ids = dict[:invalid_stream_ids]
SpeckleEntities::SpeckleEntity.new(entity, speckle_id, speckle_type, children,
valid_stream_ids, invalid_stream_ids)
end
# @param entity [Sketchup::Entity] sketchup entity to check if it was speckle entity once.
def self.speckle_entity?(entity)
return false if entity.attribute_dictionaries.nil?
return false if entity.attribute_dictionaries.to_a.empty?
entity.attribute_dictionaries.to_a.any? { |dict| dict.name == SPECKLE_BASE_OBJECT }
end
end
end
end
end
@@ -0,0 +1,30 @@
# frozen_string_literal: true
module SpeckleConnector
# Operations related to {SketchupModel}.
module SketchupModel
# Works directly with/on SketchUp Entities of different kinds (Groups, Faces, Edges, ...).
module Utils
# Static methods that work directly with Sketchup::Face objects
class FaceUtils
# A method that calculates the faces that are less than n-faces
# away from the current face. For n=1, you would get the
# neighbouring faces.
# @param edge_ary [Array<Sketchup::Edge>] the edges to look for n-adjacent faces
# @param n_adjacent [Integer] the distance from the edges (in number of faces)
# @return [Array<Sketchup::Face>] the faces that are n faces away from the given edges
def self.near_faces(edges_ary, n_adjacent = 1)
# get all the faces that are not more than a face away to the current face.
edges_ary = edges_ary.select(&:valid?)
adj_faces = []
n_adjacent.times do |_i|
new_faces = edges_ary.collect(&:faces).flatten.uniq
adj_faces += new_faces
edges_ary = new_faces.collect(&:edges).flatten.uniq - edges_ary
end
adj_faces.uniq
end
end
end
end
end
@@ -1,38 +0,0 @@
# frozen_string_literal: true
require_relative '../immutable/immutable'
require_relative '../convertors/units'
require_relative '../speckle_objects/base'
require_relative '../speckle_entities/speckle_line_entity'
require_relative '../sketchup_model/dictionary/speckle_entity_dictionary_handler'
module SpeckleConnector
module SpeckleEntities
# Speckle base entity is the state object for Sketchup::Entity and it's converted (or not yet) state.
class SpeckleBaseEntity
include Immutable::ImmutableUtils
# @return [Sketchup::Entity] sketchup entity represents {SpeckleEntity} on the model.
attr_reader :sketchup_entity
# @return [SpeckleObjects::Base] speckle object that represented on server.
attr_reader :speckle_object
# @return [Integer] application id of the sketchup entity.
attr_reader :application_id
# @param sketchup_entity [Sketchup::Entity] sketchup entity represents {SpeckleEntity} on the model.
def initialize(sketchup_model, sketchup_entity)
@sketchup_entity = sketchup_entity
@application_id = @sketchup_entity.persistent_id
@speckle_object = SpeckleObjects::Base.new
su_unit = sketchup_model.options['UnitsOptions']['LengthUnit']
@units = Converters::SKETCHUP_UNITS[su_unit]
SketchupModel::Dictionary::SpeckleEntityDictionaryHandler.write_initial_base_data(@sketchup_entity)
end
def valid?
sketchup_entity.valid?
end
end
end
end
@@ -1,19 +1,122 @@
# frozen_string_literal: true
require_relative '../speckle_entities/speckle_line_entity'
require_relative 'speckle_entity_status'
require_relative '../immutable/immutable'
require_relative '../convertors/units'
require_relative '../sketchup_model/dictionary/speckle_entity_dictionary_handler'
module SpeckleConnector
module SpeckleEntities
# Speckle entity is the state object for Sketchup::Entity and it's converted (or not yet) state.
module SpeckleEntity
def self.with_converted(skp_model, skp_entity)
# return the same object if it is already SpeckleEntity
return skp_entity if skp_entity.is_a?(SpeckleEntity)
return SpeckleBlockEntity.new(skp_model, skp_entity) if skp_entity.is_a?(Sketchup::Group)
return SpeckleBlockEntity.new(skp_model, skp_entity) if skp_entity.is_a?(Sketchup::ComponentInstance)
return SpeckleMeshEntity.new(skp_model, skp_entity) if skp_entity.is_a?(Sketchup::Face)
# Speckle base entity is the state object for Sketchup::Entity and it's converted (or not yet) state.
class SpeckleEntity
include Immutable::ImmutableUtils
# @return [Sketchup::Entity] Sketchup Entity represents {SpeckleEntity} on the model.
attr_reader :sketchup_entity
SpeckleLineEntity.new(skp_model, skp_entity) if skp_entity.is_a?(Sketchup::Edge)
# @return [String] Speckle object type.
attr_reader :speckle_type
# @return [Integer] application id of the Sketchup Entity.
attr_reader :application_id
# @return [String] id of the Speckle Base Object
attr_reader :id
# @return [Integer] total children count of the Speckle Base Object
attr_reader :total_children_count
# @return [Hash{String=>SpeckleObjects::Base}] Speckle objects belongs to edge
attr_reader :speckle_children_objects
# @return [Array<String>] speckle entity that valid on streams
attr_reader :valid_stream_ids
# @return [Array<String>] speckle entity that invalid on streams
attr_reader :invalid_stream_ids
# @return [SpeckleEntityStatus] current status of the Speckle Entity.
attr_reader :status
attr_reader :source_material, :active_diffing_stream_id
# @param sketchup_entity [Sketchup::Entity] sketchup entity represents {SpeckleEntity} on the model.
# rubocop:disable Metrics/ParameterLists
def initialize(sketchup_entity, speckle_id, speckle_type, children, valid_stream_ids, invalid_stream_ids = [])
@status = SpeckleEntityStatus::UP_TO_DATE
@source_material = sketchup_entity.material
@active_diffing_stream_id = nil
@valid_stream_ids = valid_stream_ids
@invalid_stream_ids = invalid_stream_ids
@sketchup_entity = sketchup_entity
@application_id = @sketchup_entity.persistent_id
@id = speckle_id
@total_children_count = children.length
@speckle_type = speckle_type
@speckle_children_objects = children
end
# rubocop:enable Metrics/ParameterLists
def write_initial_base_data
SketchupModel::Dictionary::SpeckleEntityDictionaryHandler
.write_initial_base_data(@sketchup_entity, application_id, id, speckle_type,
@speckle_children_objects, valid_stream_ids.first)
end
def with_up_to_date
with(:@status => SpeckleEntityStatus::UP_TO_DATE)
end
def with_invalid
valid = valid_stream_ids
sketchup_entity.set_attribute(SPECKLE_BASE_OBJECT, VALID_STREAM_IDS, [])
sketchup_entity.set_attribute(SPECKLE_BASE_OBJECT, INVALID_STREAM_IDS, valid)
with(:@valid_stream_ids => [], :@invalid_stream_ids => valid)
end
def activate_diffing(stream_id, material)
sketchup_entity.material = material
with(:@active_diffing_stream_id => stream_id)
end
def deactivate_diffing
sketchup_entity.material = @source_material
with(:@active_diffing_stream_id => nil)
end
def change_material(material)
sketchup_entity.material = material
end
def revert_material
sketchup_entity.material = source_material
end
def with_valid_stream_id(stream_id)
return self if valid_stream_ids.include?(stream_id)
invalid_ids = @invalid_stream_ids.include?(stream_id) ? @invalid_stream_ids - [stream_id] : @invalid_stream_ids
valid_ids = valid_stream_ids + [stream_id]
sketchup_entity.set_attribute(SPECKLE_BASE_OBJECT, VALID_STREAM_IDS, valid_ids)
sketchup_entity.set_attribute(SPECKLE_BASE_OBJECT, INVALID_STREAM_IDS, invalid_ids)
# if sketchup_entity.is_a?(Sketchup::Group) || sketchup_entity.is_a?(Sketchup::ComponentInstance)
# sketchup_entity.definition.set_attribute(SPECKLE_BASE_OBJECT, VALID_STREAM_IDS, valid_ids)
# sketchup_entity.definition.set_attribute(SPECKLE_BASE_OBJECT, INVALID_STREAM_IDS, invalid_ids)
# end
with(:@valid_stream_ids => @valid_stream_ids + [stream_id], :@invalid_stream_ids => invalid_ids)
end
def with_edited
with(:@status => SpeckleEntityStatus::EDITED)
end
def with_removed
with(:@status => SpeckleEntityStatus::REMOVED)
end
def valid?
sketchup_entity.valid?
end
end
end
@@ -0,0 +1,18 @@
# frozen_string_literal: true
require_relative '../immutable/immutable'
require_relative '../convertors/units'
require_relative '../sketchup_model/dictionary/speckle_entity_dictionary_handler'
module SpeckleConnector
module SpeckleEntities
module SpeckleEntityStatus
# Speckle Entity created first time with {Sketchup::Entity} before sent to server.
UP_TO_DATE = 0
# {Sketchup::Entity} that corresponds to Speckle Entity is edited after created by user.
EDITED = 1
# {Sketchup::Entity} that corresponds to Speckle Entity is removed.
REMOVED = 2
end
end
end
@@ -1,25 +0,0 @@
# frozen_string_literal: true
require_relative 'speckle_base_entity'
require_relative '../immutable/immutable'
require_relative '../speckle_objects/geometry/line'
require_relative '../sketchup_model/dictionary/speckle_entity_dictionary_handler'
module SpeckleConnector
module SpeckleEntities
# Speckle line entity is the state object for Sketchup::Entity and it's converted (or not yet) state.
class SpeckleLineEntity < SpeckleBaseEntity
include Immutable::ImmutableUtils
# @return [SpeckleObjects::Geometry::Line] speckle line object
attr_reader :speckle_object
def initialize(sketchup_model, sketchup_edge)
super(sketchup_model, sketchup_edge)
@speckle_object = SpeckleObjects::Geometry::Line.from_edge(sketchup_edge, units)
end
alias sketchup_edge sketchup_entity
end
end
end
@@ -20,9 +20,11 @@ module SpeckleConnector
# @param lens [Boolean] fov value of the view camera.
# @param units [String] units of the camera.
# @param application_id [String] application_id of the view.
# @param update_properties [Hash{Symbol=>boolean}] properties of the view.
# @param rendering_options [Hash{Symbol=>boolean}] rendering options of the view.
# rubocop:disable Metrics/ParameterLists
def initialize(name, origin, target, direction, up_direction,
is_perspective, lens, units, application_id)
is_perspective, lens, units, application_id, update_properties, rendering_options)
super(
speckle_type: SPECKLE_TYPE,
total_children_count: 0,
@@ -37,6 +39,8 @@ module SpeckleConnector
self[:isOrthogonal] = !is_perspective
self[:lens] = lens
self[:units] = units
self[:update_properties] = update_properties
self[:rendering_options] = rendering_options
end
# rubocop:enable Metrics/ParameterLists
end
@@ -40,7 +40,7 @@ module SpeckleConnector
# @param edge [Sketchup::Edge] edge to convert line.
def self.from_edge(edge, units, model_preferences)
dictionaries = {}
if model_preferences[:include_entity_attributes]
if model_preferences[:include_entity_attributes] && model_preferences[:include_edge_entity_attributes]
dictionaries = SketchupModel::Dictionary::DictionaryHandler.attribute_dictionaries_to_speckle(edge)
end
att = dictionaries.any? ? { dictionaries: dictionaries } : {}
@@ -59,11 +59,12 @@ module SpeckleConnector
)
end
# @param _state [States::State] state of the application.
# @param line [Object] object represents Speckle line.
# @param layer [Sketchup::Layer] layer to add {Sketchup::Edge} into it.
# @param entities [Sketchup::Entities] entities collection to add {Sketchup::Edge} into it.
# rubocop:disable Metrics/AbcSize
def self.to_native(line, layer, entities)
def self.to_native(state, line, layer, entities, &_convert_to_native)
if line.key?('value')
values = line['value']
points = values.each_slice(3).to_a.map { |pt| Point.to_native(pt[0], pt[1], pt[2], line['units']) }
@@ -81,6 +82,7 @@ module SpeckleConnector
.attribute_dictionaries_to_native(edge, line['sketchup_attributes']['dictionaries'])
end
end
return state, edges
end
# rubocop:enable Metrics/AbcSize
@@ -25,16 +25,15 @@ module SpeckleConnector
# @param units [String] units of the speckle mesh.
# @param render_material [Other::RenderMaterial, nil] render material of the speckle mesh.
# @param bbox [Geometry::BoundingBox] bounding box speckle object of the speckle mesh.
# @param vertices [Array] vertices of the speckle mesh.
# @param faces [Array] faces of the speckle mesh.
# @param sketchup_attributes [Hash] additional information about speckle mesh.
# rubocop:disable Metrics/ParameterLists
def initialize(units:, render_material:, bbox:, vertices:, faces:, sketchup_attributes:)
def initialize(units:, render_material:, vertices:, faces:, sketchup_attributes:, application_id: nil)
super(
speckle_type: SPECKLE_TYPE,
total_children_count: 0,
application_id: nil,
application_id: application_id,
id: nil
)
@vertices = []
@@ -42,7 +41,7 @@ module SpeckleConnector
@units = units
self[:units] = units
self[:renderMaterial] = render_material
self[:bbox] = bbox
# self[:bbox] = bbox
self[:'@(31250)vertices'] = vertices
self[:'@(62500)faces'] = faces
self[:sketchup_attributes] = sketchup_attributes if sketchup_attributes.any?
@@ -53,9 +52,11 @@ module SpeckleConnector
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
def self.to_native(sketchup_model, mesh, layer, entities, model_preferences)
# rubocop:disable Metrics/PerceivedComplexity:
def self.to_native(state, mesh, layer, entities, &convert_to_native)
model_preferences = state.user_state.preferences[:model]
# Get soft? flag of {Sketchup::Edge} object to understand smoothness of edge.
is_soften = get_soften_setting(mesh)
is_soften = get_soften_setting(mesh, entities)
smooth_flags = is_soften ? 4 : 1
# Get native points to add polygon into native mesh.
points = get_native_points(mesh)
@@ -69,8 +70,17 @@ module SpeckleConnector
indices = faces.shift(num_pts)
native_mesh.add_polygon(indices.map { |index| points[index] })
end
material = Other::RenderMaterial.to_native(sketchup_model, mesh['renderMaterial'])
entities.add_faces_from_mesh(native_mesh, smooth_flags, material)
state, _materials = Other::RenderMaterial.to_native(state, mesh['renderMaterial'],
layer, entities, &convert_to_native)
# Find and assign material if exist
unless mesh['renderMaterial'].nil?
material_name = mesh['renderMaterial']['name'] || mesh['renderMaterial']['id']
# Retrieve material from state
material = state.sketchup_state.materials.by_id(material_name)
end
# 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)
added_faces.each do |face|
face.layer = layer
@@ -80,18 +90,22 @@ module SpeckleConnector
end
end
# Merge only added faces in this scope
Converters::CleanUp.merge_coplanar_faces(added_faces) if model_preferences[:merge_coplanar_faces]
native_mesh
if model_preferences[:merge_coplanar_faces]
added_faces = Converters::CleanUp.merge_coplanar_faces(added_faces)
end
return state, added_faces
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity:
# @param face [Sketchup::Face] face to convert mesh
# rubocop:disable Style/MultilineTernaryOperator
# rubocop:disable Metrics/CyclomaticComplexity
def self.from_face(face, units, model_preferences)
dictionaries = {}
if model_preferences[:include_entity_attributes]
if model_preferences[:include_entity_attributes] && model_preferences[:include_face_entity_attributes]
dictionaries = SketchupModel::Dictionary::DictionaryHandler.attribute_dictionaries_to_speckle(face)
end
has_any_soften_edge = face.edges.any?(&:soft?)
@@ -101,17 +115,18 @@ module SpeckleConnector
units: units,
render_material: face.material.nil? && face.back_material.nil? ? nil : Other::RenderMaterial
.from_material(face.material || face.back_material),
bbox: Geometry::BoundingBox.from_bounds(face.bounds, units),
vertices: [], # mesh.nil? ? face_vertices_to_array(face, units) : mesh_points_to_array(mesh, units),
faces: [], # mesh.nil? ? face_indices_to_array(face, 0) : mesh_faces_to_array(mesh, -1),
# face_edge_flags: [], # mesh.nil? ? face_edge_flags_to_array(face) : mesh_edge_flags_to_array(mesh),
sketchup_attributes: att
# bbox: Geometry::BoundingBox.from_bounds(face.bounds, units),
vertices: [],
faces: [],
sketchup_attributes: att,
application_id: face.persistent_id
)
speckle_mesh.face_to_mesh(face)
speckle_mesh.update_mesh
speckle_mesh
end
# rubocop:enable Style/MultilineTernaryOperator
# rubocop:enable Metrics/CyclomaticComplexity
def face_to_mesh(face)
mesh = face.loops.count > 1 ? face.mesh : nil
@@ -184,12 +199,23 @@ module SpeckleConnector
end
end
def self.get_soften_setting(mesh)
if mesh['sketchup_attributes'].nil?
true
else
mesh['sketchup_attributes']['is_soften'].nil? ? true : mesh['sketchup_attributes']['is_soften']
DEFINITIONS_WILL_BE_SOFT_EDGE = %w[
Furniture-
Topography-
Column-
Lighting Fixtures-
Railings-
Roofs-
].freeze
# @param mesh [Object] speckle mesh object
# @param entities [Sketchup::Entities] sketchup entities that mesh will be created in it as face.
def self.get_soften_setting(mesh, entities)
unless mesh['sketchup_attributes'].nil?
return mesh['sketchup_attributes']['is_soften'].nil? ? true : mesh['sketchup_attributes']['is_soften']
end
return DEFINITIONS_WILL_BE_SOFT_EDGE.any? { |def_name| entities.parent.name.include?(def_name) }
end
def self.get_native_points(mesh)
@@ -2,7 +2,6 @@
require_relative 'length'
require_relative '../base'
require_relative '../../typescript/typescript_object'
module SpeckleConnector
module SpeckleObjects
@@ -17,12 +17,11 @@ module SpeckleConnector
SPECKLE_TYPE = 'Objects.Other.BlockDefinition'
# @param geometry [Object] geometric definition of the block.
# @param base_point [Geometry::Point] base point of the block definition.
# @param name [String] name of the block definition.
# @param units [String] units of the block definition.
# @param application_id [String, NilClass] application id of the block definition.
# rubocop:disable Metrics/ParameterLists
def initialize(geometry:, base_point:, name:, units:, always_face_camera:, sketchup_attributes: {},
def initialize(geometry:, name:, units:, always_face_camera:, sketchup_attributes: {},
application_id: nil)
super(
speckle_type: SPECKLE_TYPE,
@@ -32,11 +31,10 @@ module SpeckleConnector
)
self[:units] = units
self[:name] = name
self[:basePoint] = base_point
self[:always_face_camera] = always_face_camera
self[:sketchup_attributes] = sketchup_attributes if sketchup_attributes.any?
# FIXME: Since geometry sends with @ as detached, block basePlane renders on viewer.
self['@geometry'] = geometry
self['@@geometry'] = geometry
end
# rubocop:enable Metrics/ParameterLists
@@ -47,58 +45,123 @@ module SpeckleConnector
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/MethodLength
def self.from_definition(definition, units, definitions, preferences, &convert)
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/ParameterLists
def self.from_definition(definition, units, definitions, preferences, speckle_state, parent, &convert)
guid = definition.guid
return definitions[guid] if definitions.key?(guid)
dictionaries = {}
if preferences[:model][:include_entity_attributes]
dictionaries = SketchupModel::Dictionary::DictionaryHandler.attribute_dictionaries_to_speckle(definition)
if definition.group?
if preferences[:model][:include_group_entity_attributes]
dictionaries = SketchupModel::Dictionary::DictionaryHandler
.attribute_dictionaries_to_speckle(definition)
end
elsif preferences[:model][:include_component_entity_attributes]
dictionaries = SketchupModel::Dictionary::DictionaryHandler.attribute_dictionaries_to_speckle(definition)
end
end
att = dictionaries.any? ? { dictionaries: dictionaries } : {}
# TODO: Solve logic
geometry = if definition.entities[0].is_a?(Sketchup::Edge) || definition.entities[0].is_a?(Sketchup::Face)
group_entities_to_speckle(definition, units, definitions, preferences, &convert)
new_speckle_state, geo = group_entities_to_speckle(
definition, preferences, speckle_state, parent, &convert
)
speckle_state = new_speckle_state
geo
else
definition.entities.map do |entity|
convert.call(entity, preferences) unless entity.is_a?(Sketchup::Edge) && entity.faces.any?
next if entity.is_a?(Sketchup::Edge) && entity.faces.any?
new_speckle_state, converted = convert.call(entity, preferences,
speckle_state,
definition.persistent_id)
speckle_state = new_speckle_state
converted
end
end
# FIXME: Decide how to approach base point of the definition instead origin.
BlockDefinition.new(
block_definition = BlockDefinition.new(
units: units,
name: definition.name,
base_point: Geometry::Point.new(0, 0, 0, units),
geometry: geometry,
always_face_camera: definition.behavior.always_face_camera?,
sketchup_attributes: att,
application_id: guid
application_id: definition.persistent_id.to_s
)
return speckle_state, block_definition
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/ParameterLists
def self.built_element?(definition_object)
definition_object['speckle_type'].include?('Objects.BuiltElements')
end
def self.unique_element?(definition_object)
UNIQUE_DEFINITIONS.any? { |d| definition_object['speckle_type'].include?(d) }
end
UNIQUE_DEFINITIONS = %w[
Objects.BuiltElements.Wall
Objects.BuiltElements.Floor
Objects.BuiltElements.Ceiling
Objects.BuiltElements.Column
Objects.BuiltElements.Beam
].freeze
def self.get_definition_name(def_obj)
return def_obj['name'] unless def_obj['name'].nil?
# Check unique elements when instancing implemented to add it with element id.
if built_element?(def_obj) && unique_element?(def_obj)
return "#{def_obj['category']}-#{def_obj['family']}-#{def_obj['type']}-#{def_obj['elementId']}"
end
return "#{def_obj['category']}-#{def_obj['family']}-#{def_obj['type']}" if built_element?(def_obj)
return "def::#{def_obj['applicationId']}"
end
# Finds or creates a component definition from the geometry and the given name
# @param sketchup_model [Sketchup::Model] sketchup model to check block definitions.
# @param state [States::State] state of the application.
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/ParameterLists
def self.to_native(sketchup_model, geometry, layer, name, always_face_camera, model_preferences,
sketchup_attributes, application_id = '', &convert)
definition = sketchup_model.definitions[name]
return definition if definition && (definition.name == name || definition.guid == application_id)
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
def self.to_native(state, definition_obj, layer, _entities, &convert_to_native)
sketchup_model = state.sketchup_state.sketchup_model
definition&.entities&.clear!
definition ||= sketchup_model.definitions.add(name)
definition.layer = layer
if geometry.is_a?(Array)
geometry.each { |obj| convert.call(obj, layer, model_preferences, definition.entities) }
# FIXME: Check later this is a valid check or not. Maybe unnecessary? If necessary document it!
# Check definitions from sketchup_model with name and application id
definition_name = get_definition_name(definition_obj)
application_id = definition_obj['applicationId']
definition = sketchup_model.definitions[definition_name]
if definition && (definition.name == definition_name || definition.guid == application_id)
return state, [definition]
end
if geometry.is_a?(Hash) && !geometry['speckle_type'].nil?
convert.call(geometry, layer, model_preferences, definition.entities)
geometry = definition_obj['geometry'] || definition_obj['@geometry'] || definition_obj['displayValue']
always_face_camera = definition_obj['always_face_camera'].nil? ? false : definition_obj['always_face_camera']
sketchup_attributes = definition_obj['sketchup_attributes']
definition&.entities&.clear!
definition ||= sketchup_model.definitions.add(definition_name)
definition.layer = layer
if geometry.is_a?(Array)
geometry.each do |obj|
state = convert_to_native.call(state, obj, layer, definition.entities)
end
end
if geometry.is_a?(Hash) && !definition_obj['speckle_type'].nil?
state = convert_to_native.call(state, geometry, layer, definition.entities)
end
# puts("definition finished: #{name} (#{application_id})")
# puts(" entity count: #{definition.entities.count}")
@@ -107,45 +170,58 @@ module SpeckleConnector
SketchupModel::Dictionary::DictionaryHandler
.attribute_dictionaries_to_native(definition, sketchup_attributes['dictionaries'])
end
definition
return state, [definition]
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/ParameterLists
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def self.group_entities_to_speckle(definition, units, definitions, preferences, &convert)
def self.group_entities_to_speckle(definition, preferences, speckle_state, parent, &convert)
orphan_edges = definition.entities.grep(Sketchup::Edge).filter { |edge| edge.faces.none? }
lines = orphan_edges.collect do |orphan_edge|
Geometry::Line.from_edge(orphan_edge, units, preferences[:model])
new_speckle_state, converted = convert.call(orphan_edge, preferences, speckle_state, parent)
speckle_state = new_speckle_state
converted
end
nested_blocks = definition.entities.grep(Sketchup::ComponentInstance).collect do |component_instance|
BlockInstance.from_component_instance(component_instance, units, definitions, preferences, &convert)
new_speckle_state, converted = convert.call(component_instance, preferences, speckle_state, parent)
speckle_state = new_speckle_state
converted
end
nested_groups = definition.entities.grep(Sketchup::Group).collect do |group|
BlockInstance.from_group(group, units, definitions, preferences, &convert)
new_speckle_state, converted = convert.call(group, preferences, speckle_state, parent)
speckle_state = new_speckle_state
converted
end
if preferences[:model][:combine_faces_by_material]
mesh_groups = {}
definition.entities.grep(Sketchup::Face).collect do |face|
group_meshes_by_material(face, mesh_groups, units, preferences[:model])
new_speckle_state = group_meshes_by_material(
face, mesh_groups, speckle_state, preferences, parent, &convert
)
speckle_state = new_speckle_state
end
# Update mesh overwrites points and polygons into base object.
mesh_groups.each { |_, mesh| mesh.update_mesh }
mesh_groups.each { |_, mesh| mesh.first.update_mesh }
lines + nested_blocks + nested_groups + mesh_groups.values
return speckle_state, lines + nested_blocks + nested_groups + mesh_groups.values
else
meshes = definition.entities.grep(Sketchup::Face).collect do |face|
Geometry::Mesh.from_face(face, units, preferences[:model])
meshes = []
definition.entities.grep(Sketchup::Face).collect do |face|
new_speckle_state, converted = convert.call(face, preferences, speckle_state, parent)
meshes.append(converted)
speckle_state = new_speckle_state
end
lines + nested_blocks + nested_groups + meshes
return speckle_state, lines + nested_blocks + nested_groups + meshes
end
end
# rubocop:enable Metrics/AbcSize
@@ -153,20 +229,26 @@ module SpeckleConnector
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
def self.group_meshes_by_material(face, mat_groups, units, model_preferences)
# rubocop:disable Metrics/ParameterLists
def self.group_meshes_by_material(face, mesh_groups, speckle_state, preferences, parent, &convert)
# convert material
mat_id = get_mesh_group_id(face, model_preferences)
mat_groups[mat_id] = Geometry::Mesh.from_face(face, units, model_preferences) unless mat_groups.key?(mat_id)
mat_group = mat_groups[mat_id]
mat_group.face_to_mesh(face)
mesh_group_id = get_mesh_group_id(face, preferences[:model])
new_speckle_state, converted = convert.call(face, preferences, speckle_state, parent)
mesh_groups[mesh_group_id] = converted unless mesh_groups.key?(mesh_group_id)
mesh_group = mesh_groups[mesh_group_id]
mesh_group[0].face_to_mesh(face)
mesh_group[1].append(face)
new_speckle_state
end
# rubocop:enable Metrics/ParameterLists
# Mesh group id helps to determine how to group faces into meshes.
# @param face [Sketchup::Face] face to get mesh group id.
def self.get_mesh_group_id(face, model_preferences)
if model_preferences[:include_entity_attributes]
has_attribute_dictionary = !(face.attribute_dictionaries.nil? || face.attribute_dictionaries.first.nil?)
return face.persistent_id.to_s if has_attribute_dictionary
if model_preferences[:include_entity_attributes] &&
model_preferences[:include_face_entity_attributes] &&
attribute_dictionary?(face)
return face.persistent_id.to_s
end
material = face.material || face.back_material
@@ -174,6 +256,23 @@ module SpeckleConnector
return material.entityID.to_s
end
def self.attribute_dictionary?(face)
any_attribute_dictionary = !(face.attribute_dictionaries.nil? || face.attribute_dictionaries.first.nil?)
return any_attribute_dictionary unless any_attribute_dictionary
# If there are any attribute dictionary, then make sure that they are not ignored ones.
all_attribute_dictionary_ignored = face.attribute_dictionaries.all? do |dict|
ignored_dictionaries.include?(dict.name)
end
!all_attribute_dictionary_ignored
end
def self.ignored_dictionaries
[
'Speckle_Base_Object'
]
end
end
end
end
@@ -38,40 +38,54 @@ module SpeckleConnector
self[:transform] = transform
self[:sketchup_attributes] = sketchup_attributes if sketchup_attributes.any?
# FIXME: Since blockDefinition sends with @ as detached, block basePlane renders on viewer.
self['@blockDefinition'] = block_definition
self['@@definition'] = block_definition
end
# rubocop:enable Metrics/ParameterLists
# @param group [Sketchup::Group] group to convert Speckle BlockInstance
def self.from_group(group, units, component_defs, preferences, &convert)
def self.from_group(group, units, preferences, speckle_state, &convert)
new_speckle_state, block_definition = convert.call(group.definition, preferences, speckle_state,
group.persistent_id)
speckle_state = new_speckle_state
dictionaries = {}
if preferences[:model][:include_entity_attributes]
if preferences[:model][:include_entity_attributes] && preferences[:model][:include_group_entity_attributes]
dictionaries = SketchupModel::Dictionary::DictionaryHandler.attribute_dictionaries_to_speckle(group)
end
att = dictionaries.any? ? { dictionaries: dictionaries } : {}
BlockInstance.new(
block_instance = BlockInstance.new(
units: units,
is_sketchup_group: true,
name: group.name == '' ? nil : group.name,
render_material: group.material.nil? ? nil : RenderMaterial.from_material(group.material),
transform: Other::Transform.from_transformation(group.transformation, units),
block_definition: BlockDefinition.from_definition(group.definition, units, component_defs,
preferences, &convert),
block_definition: block_definition,
sketchup_attributes: att,
application_id: group.guid
)
return speckle_state, block_instance
end
# @param component_instance [Sketchup::ComponentInstance] component instance to convert Speckle BlockInstance
# rubocop:disable Metrics/MethodLength
def self.from_component_instance(component_instance, units, component_defs, preferences, &convert)
def self.from_component_instance(component_instance, units, preferences, speckle_state, &convert)
new_speckle_state, block_definition = convert.call(
component_instance.definition,
preferences,
speckle_state,
component_instance.persistent_id
)
speckle_state = new_speckle_state
dictionaries = {}
if preferences[:model][:include_entity_attributes]
if preferences[:model][:include_entity_attributes] &&
preferences[:model][:include_component_entity_attributes]
dictionaries = SketchupModel::Dictionary::DictionaryHandler
.attribute_dictionaries_to_speckle(component_instance)
end
att = dictionaries.any? ? { dictionaries: dictionaries } : {}
BlockInstance.new(
block_instance = BlockInstance.new(
units: units,
is_sketchup_group: false,
name: component_instance.name == '' ? nil : component_instance.name,
@@ -81,46 +95,44 @@ module SpeckleConnector
RenderMaterial.from_material(component_instance.material)
end,
transform: Other::Transform.from_transformation(component_instance.transformation, units),
block_definition: BlockDefinition.from_definition(component_instance.definition, units, component_defs,
preferences, &convert),
block_definition: block_definition,
sketchup_attributes: att,
application_id: component_instance.guid
application_id: component_instance.persistent_id.to_s
)
return speckle_state, block_instance
end
# rubocop:enable Metrics/MethodLength
# Creates a component instance from a block
# @param sketchup_model [Sketchup::Model] sketchup model to check block definitions.
# @param state [States::State] state of the application.
# @param block [Object] block object that represents Speckle block.
# @param layer [Sketchup::Layer] layer to add {Sketchup::Edge} into it.
# @param entities [Sketchup::Entities] entities collection to add {Sketchup::Edge} into it.
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/ParameterLists
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def self.to_native(sketchup_model, block, layer, entities, model_preferences, &convert)
def self.to_native(state, block, layer, entities, &convert_to_native)
# is_group = block.key?("is_sketchup_group") && block["is_sketchup_group"]
# something about this conversion is freaking out if nested block geo is a group
# so this is set to false always until I can figure this out
is_group = false
# is_group = block['is_sketchup_group']
block_definition = block['@blockDefinition'] || block['blockDefinition']
geometry = block_definition['@geometry'] || block_definition['geometry']
definition = BlockDefinition.to_native(
sketchup_model,
geometry,
# NOTE: nil checks for backward compatibility
block_definition = block['definition'] || block['blockDefinition'] || block['@blockDefinition']
state, _definitions = BlockDefinition.to_native(
state,
block_definition,
layer,
block_definition['name'],
block_definition['always_face_camera'].nil? ? false : block_definition['always_face_camera'],
model_preferences,
block_definition['sketchup_attributes'],
block_definition['applicationId'],
&convert
entities,
&convert_to_native
)
instance_name = block['name'].nil? || block['name'].empty? ? block['id'] : block['name']
t_arr = block['transform'].is_a?(Hash) ? block['transform']['value'] : block['transform']
definition = state.sketchup_state.sketchup_model
.definitions[BlockDefinition.get_definition_name(block_definition)]
t_arr = get_transform_matrix(block)
transform = Other::Transform.to_native(t_arr, block['units'])
instance = if is_group
# rubocop:disable SketchupSuggestions/AddGroup
@@ -136,28 +148,70 @@ module SpeckleConnector
# erase existing instances after creation and before rename because you can't have definitions
# without instances
find_and_erase_existing_instance(definition, instance_name, block['applicationId'])
find_and_erase_existing_instance(definition, block['id'], block['applicationId'])
puts("Failed to create instance for speckle block instance #{block['id']}") if instance.nil?
# Transform already applied to instance unless is group
instance.transformation = transform if is_group
instance.material = Other::RenderMaterial.to_native(sketchup_model, block['renderMaterial'])
instance.name = instance_name
state, _materials = Other::RenderMaterial.to_native(state, block['renderMaterial'],
layer, entities, &convert_to_native)
# Retrieve material from state
unless block['renderMaterial'].nil?
material_name = block['renderMaterial']['name'] || block['renderMaterial']['id']
material = state.sketchup_state.materials.by_id(material_name)
instance.material = material
end
instance.name = block['name'] unless block['name'].nil?
unless block['sketchup_attributes'].nil?
SketchupModel::Dictionary::DictionaryHandler
.attribute_dictionaries_to_native(instance, block['sketchup_attributes']['dictionaries'])
end
instance
return state, [instance, definition]
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/ParameterLists
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
def self.get_transform_matrix(block)
if block['transform'].is_a?(Hash)
block['transform']['matrix'] || block['transform']['value']
else
block['transform']
end
end
def self.instance_to_speckle_entity(state, instance, speckle_instance, stream_id)
return state unless state.user_state.user_preferences[:register_speckle_entity]
speckle_id = speckle_instance['id']
speckle_type = speckle_instance['speckle_type']
children = speckle_instance['__closure'].nil? ? [] : speckle_instance['__closure']
ent = SpeckleEntities::SpeckleEntity.new(instance, speckle_id, speckle_type, children, [stream_id])
ent.write_initial_base_data
new_speckle_state = state.speckle_state.with_speckle_entity(ent)
state.with_speckle_state(new_speckle_state)
end
# takes a component definition and finds and erases the first instance with the matching name
# (and optionally the applicationId)
def self.find_and_erase_existing_instance(definition, name, app_id = '')
definition.instances.find { |ins| ins.name == name || ins.guid == app_id }&.erase!
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/CyclomaticComplexity
def self.find_and_erase_existing_instance(definition, upcoming_speckle_id, upcoming_app_id = '')
definition.instances.find do |ins|
next if ins.attribute_dictionaries.nil?
next if ins.attribute_dictionaries.to_a.empty?
next if ins.attribute_dictionaries.to_a.none? { |dict| dict.name == SPECKLE_BASE_OBJECT }
dict = ins.attribute_dictionaries.to_a.find { |d| d.name == SPECKLE_BASE_OBJECT }
speckle_id = dict[:speckle_id]
application_id = dict[:application_id]
speckle_id == upcoming_speckle_id || application_id == upcoming_app_id
end&.erase!
end
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity
end
end
end
@@ -0,0 +1,25 @@
# frozen_string_literal: true
module SpeckleConnector
module SpeckleObjects
module Others
# Color object transformations between sketchup and speckle.
class Color
# @param color [Sketchup::Color] color to convert speckle object
def self.to_speckle(color)
{
red: color.red,
green: color.green,
blue: color.blue,
alpha: color.alpha
}
end
def self.to_native(speckle_color)
Sketchup::Color.new(speckle_color['red'], speckle_color['green'],
speckle_color['blue'], speckle_color['alpha'])
end
end
end
end
end
@@ -0,0 +1,59 @@
# frozen_string_literal: true
require_relative 'render_material'
require_relative 'transform'
require_relative 'block_definition'
require_relative '../../immutable/immutable'
require_relative '../../ext/immutable_ruby/hash'
require_relative '../base'
require_relative '../geometry/bounding_box'
require_relative '../../sketchup_model/dictionary/dictionary_handler'
module SpeckleConnector
module SpeckleObjects
module Other
# DisplayValue object definition for Speckle that represents as BlockInstance in Sketchup.
class DisplayValue
# Creates a component definition and instance from a speckle object with a display value
# @param state [States::State] state of the application.
def self.to_native(state, obj, layer, entities, &convert_to_native)
# Switch displayValue with geometry
obj = collect_definition_geometries(obj)
state, _definitions = BlockDefinition.to_native(
state,
obj,
layer,
entities,
&convert_to_native
)
definition = state.sketchup_state.sketchup_model.definitions[BlockDefinition.get_definition_name(obj)]
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, units)
instance = entities.add_instance(definition, transform)
instance.name = obj['name'] unless obj['name'].nil?
return state, [instance, definition]
end
def self.collect_definition_geometries(obj)
obj['geometry'] = obj['displayValue']
if !obj['elements'].nil? && obj['elements'].is_a?(Array)
obj['elements'].each do |element|
# Mullions is a special case here, they are extracted as base object with @displayValue from revit..
if element['@displayValue'].nil?
obj['geometry'].append(element)
else
obj['geometry'] += element['@displayValue']
end
end
end
obj
end
end
end
end
end
@@ -1,7 +1,6 @@
# frozen_string_literal: true
require_relative '../base'
require_relative '../../typescript/typescript_object'
module SpeckleConnector
module SpeckleObjects
@@ -50,21 +49,25 @@ module SpeckleConnector
)
end
# @param sketchup_model [Sketchup::Model] active model on the sketchup.
def self.to_native(sketchup_model, render_material)
return if render_material.nil?
# @param state [States::State] state of the application.
def self.to_native(state, render_material, _layer, _entities, &_convert_to_native)
return state, [] if render_material.nil?
sketchup_model = state.sketchup_state.sketchup_model
materials = state.sketchup_state.materials
# return material with same name if it exists
name = render_material['name'] || render_material['id']
material = sketchup_model.materials[name]
return material if material
material = materials.by_id(name)
return state, [material] if material
# create a new sketchup material
material = sketchup_model.materials.add(name)
material.alpha = render_material['opacity']
argb = render_material['diffuse']
material.color = Sketchup::Color.new((argb >> 16) & 255, (argb >> 8) & 255, argb & 255, (argb >> 24) & 255)
material
new_sketchup_state = state.sketchup_state.with_materials(materials.add_material(name, material))
return state.with_sketchup_state(new_sketchup_state), [material]
end
end
end
@@ -0,0 +1,24 @@
# frozen_string_literal: true
require_relative 'color'
module SpeckleConnector
module SpeckleObjects
module Others
# Rendering options for scenes.
class RenderingOptions
# @param options [Sketchup::RenderingOptions] rendering options to convert speckle object
def self.to_speckle(options)
options.to_h.collect do |option_prop, option_value|
speckle_value = if option_value.is_a?(Sketchup::Color)
Color.to_speckle(option_value)
else
option_value
end
[option_prop.to_sym, speckle_value]
end.to_h
end
end
end
end
end
@@ -1,6 +1,8 @@
# frozen_string_literal: true
require_relative '../immutable/immutable'
require_relative '../sketchup_model/materials/materials'
require_relative '../sketchup_model/definitions/definitions'
module SpeckleConnector
module States
@@ -10,9 +12,17 @@ module SpeckleConnector
# @return [Sketchup::Model] active model on the sketchup
attr_reader :sketchup_model
# @return [SketchupModel::Materials] materials by their id
attr_reader :materials
# @return [SketchupModel::Definitions] definitions by their guid and name
attr_reader :definitions
# @param sketchup_model [Sketchup::Model] active model on the sketchup
def initialize(sketchup_model)
@sketchup_model = sketchup_model
@materials = SketchupModel::Materials.from_sketchup_model(sketchup_model)
@definitions = SketchupModel::Definitions.from_sketchup_model(sketchup_model)
end
# @return [Integer] length units code of the sketchup model.
@@ -20,6 +30,10 @@ module SpeckleConnector
def length_units
sketchup_model.options['UnitsOptions']['LengthUnit']
end
def with_materials(new_materials)
with(:@materials => new_materials)
end
end
end
end
+47 -1
View File
@@ -2,6 +2,7 @@
require_relative '../immutable/immutable'
require_relative '../callbacks/callback_message'
require_relative '../speckle_entities/speckle_entity'
module SpeckleConnector
module States
@@ -9,19 +10,33 @@ module SpeckleConnector
class SpeckleState
include Immutable::ImmutableUtils
# @return [ImmutableHash{Integer=>SpeckleEntities::SpeckleEntity}] persistent_id of the sketchup entity and
# corresponding speckle entity
attr_reader :speckle_entities
# @return [Array] accounts on appdata.
attr_reader :accounts
# @return [Hash{Class => Observer}] the observer objects that are used to attach to objects in Sketchup to collect
# events that are triggered from Sketchup
attr_reader :observers
# @return [Hash] queue to send to server.
attr_reader :message_queue
# @return [Hash] stream queue to send to server.
attr_reader :stream_queue
def initialize(accounts, queue, stream_queue)
# @return [Relations::ManyToOneRelation] relations between objects.
attr_accessor :relation
def initialize(accounts, observers, queue, stream_queue)
@accounts = accounts
@observers = observers
@message_queue = queue
@stream_queue = stream_queue
@speckle_entities = Immutable::EmptyHash
@relation = Relations::ManyToOneRelation.new
end
# @param callback_name [String] name of the callback command
@@ -33,9 +48,40 @@ module SpeckleConnector
with(:@message_queue => new_queue)
end
def with_invalid_streams_queue
new_queue = message_queue.merge({ "updateInvalidStreams":
"updateInvalidStreams(#{JSON.generate(invalid_streams)})" })
with(:@message_queue => new_queue)
end
def with_empty_invalid_streams_queue
new_queue = message_queue.merge({ "updateInvalidStreams":
"updateInvalidStreams(#{JSON.generate([])})" })
with(:@message_queue => new_queue)
end
def with_accounts(new_accounts)
with(:@accounts => new_accounts)
end
def with_speckle_entity(traversed_entity)
new_speckle_entities = speckle_entities.put(traversed_entity.application_id, traversed_entity)
with_speckle_entities(new_speckle_entities)
end
def with_speckle_entities(new_speckle_entities)
with(:@speckle_entities => new_speckle_entities)
end
def with_relation(new_relation)
with(:@relation => new_relation)
end
def invalid_streams
speckle_entities.collect do |_id, speckle_entity|
speckle_entity.invalid_stream_ids
end.reduce([], :concat).uniq
end
end
end
end
+4
View File
@@ -45,6 +45,10 @@ module SpeckleConnector
with(:@speckle_state => new_speckle_state)
end
def with_sketchup_state(new_sketchup_state)
with(:@sketchup_state => new_sketchup_state)
end
def with_user_state(new_user_state)
with(:@user_state => new_user_state)
end
@@ -15,6 +15,14 @@ module SpeckleConnector
@preferences = preferences
end
def user_preferences
@preferences[:user]
end
def model_preferences
@preferences[:model]
end
def with_preferences(new_preferences)
with(:@preferences => new_preferences)
end
@@ -1,57 +0,0 @@
# frozen_string_literal: true
require 'json'
module SpeckleConnector
module Typescript
# Object to help convert object attributes to JSON and by checking types.
class TypescriptObject
# @param attributes [Hash{Symbol=>Object}] attributes are given as key value pairs
def initialize(**attributes)
@attributes = attributes
check_attributes
end
# @return [String] the JSON representation of the object
def to_json(*options)
@attributes.to_json(*options)
end
def to_h(*options)
JSON.parse(to_json(*options), { symbolize_names: true })
end
private
# rubocop:disable Metrics/CyclomaticComplexity
def check_attributes
attribute_types.each do |key, class_or_classes|
value = @attributes[key]
case class_or_classes
when Array
is_class_correct = class_or_classes.any? do |klass|
raise "#{klass} is not a class" unless klass.is_a? Class
value.is_a? klass
end
raise "attribute #{key} is of class #{value.class} and not #{class_or_classes}" unless is_class_correct
when Class
raise ArgumentError, "#{class_or_classes} should be class" unless class_or_classes.is_a? Class
unless value.is_a? class_or_classes
raise ArgumentError,
"attribute #{key} is of class #{value.class} and not #{class_or_classes}"
end
else
raise ArgumentError, "#{class_or_classes} should be class or array of classes"
end
end
end
# rubocop:enable Metrics/CyclomaticComplexity
def attribute_types
raise NotImplementedError, 'Implement in child class'
end
end
end
end
+1 -1
View File
@@ -97,7 +97,7 @@ module SpeckleConnector
commands = CommandParser.parse_commands(data)
commands.each do |cmd|
puts '### COMMAND CALLED BY DIALOG ###'
puts "name: #{cmd.name}, data: #{cmd.data}"
puts "name: #{cmd.name}"
@ready = true if cmd.name == DIALOG_READY
@commands[cmd.name].run(cmd.data)
end
+7 -1
View File
@@ -13,11 +13,14 @@ require_relative '../commands/remove_stream'
require_relative '../commands/notify_connected'
require_relative '../commands/user_preferences_updated'
require_relative '../commands/model_preferences_updated'
require_relative '../commands/activate_diffing'
require_relative '../actions/reload_accounts'
require_relative '../actions/load_saved_streams'
require_relative '../actions/init_local_accounts'
require_relative '../actions/collect_preferences'
require_relative '../actions/deactivate_diffing'
require_relative '../actions/collect_versions'
module SpeckleConnector
module Ui
@@ -65,8 +68,11 @@ module SpeckleConnector
remove_stream: Commands::RemoveStream.new(@app),
notify_connected: Commands::NotifyConnected.new(@app),
collect_preferences: Commands::ActionCommand.new(@app, Actions::CollectPreferences),
collect_versions: Commands::ActionCommand.new(@app, Actions::CollectVersions),
user_preferences_updated: Commands::UserPreferencesUpdated.new(@app),
model_preferences_updated: Commands::ModelPreferencesUpdated.new(@app)
model_preferences_updated: Commands::ModelPreferencesUpdated.new(@app),
activate_diffing: Commands::ActivateDiffing.new(@app),
deactivate_diffing: Commands::ActionCommand.new(@app, Actions::DeactivateDiffing)
}.freeze
end
end
@@ -0,0 +1,32 @@
# frozen_string_literal: true
require_relative '../../test_helper'
require_relative '../../../speckle_connector/src/states/speckle_state'
require_relative '../../../speckle_connector/src/speckle_objects/geometry/point'
require_relative '../../../speckle_connector/src/speckle_objects/geometry/line'
require_relative '../../../speckle_connector/src/convertors/base_object_serializer'
module SpeckleConnector
module Converters
class BaseObjectSerializerTest < Minitest::Test
def setup
# Do nothing
end
def teardown
# Do nothing
end
def test_base_initialize
speckle_state = States::SpeckleState.new({}, {}, {}, {})
start_point = SpeckleObjects::Geometry::Point.new(0.0, 0.0, 0.0, 'm')
end_point = SpeckleObjects::Geometry::Point.new(10.0, 10.0, 0.0, 'm')
line = SpeckleObjects::Geometry::Line.test_line(start_point, end_point, 'm')
serializer = Converters::BaseObjectSerializer.new
new_speckle_state, id, traversed = serializer.serialize(line, speckle_state)
assert_equal(id, "a53c02860be04238514f1a5b00883fe2")
end
end
end
end
+21 -4
View File
@@ -80,7 +80,11 @@
</v-menu>
</v-app-bar>
<create-stream-dialog v-if="accounts().length !== 0"/>
<create-stream-dialog
v-if="accounts().length !== 0"
:account-id="activeAccount().userInfo.id"
:server-url="activeAccount().serverInfo.url"
/>
<v-container v-if="accounts().length !== 0" fluid>
<router-view :stream-search-query="streamSearchQuery" />
@@ -104,13 +108,19 @@ global.collectPreferences = function (preferences) {
bus.$emit('update-preferences', preferences)
}
global.collectVersions = function (versions) {
let vers = JSON.parse(versions)
localStorage.setItem('hostAppVersion', vers.sketchup)
localStorage.setItem('speckleVersion', vers.speckle)
}
global.loadAccounts = function (accounts) {
console.log('>>> SpeckleSketchup: Loading accounts', accounts)
localStorage.setItem('localAccounts', JSON.stringify(accounts))
let uuid = localStorage.getItem('uuid')
if (accounts.length !== 0){
if (uuid) {
global.setSelectedAccount(accounts.find((acct) => acct['userInfo']['id'] == uuid))
global.setSelectedAccount(accounts.find((acct) => acct['userInfo']['id'] === uuid))
} else {
global.setSelectedAccount(accounts.find((acct) => acct['isDefault']))
}
@@ -176,15 +186,22 @@ export default {
this.$vuetify.theme.dark = this.preferences.user.dark_theme
})
// Collect versions to inform mixpanel
sketchup.exec({name: "collect_versions", data: {}})
// Collect preferences to render UI according to it
sketchup.exec({name: "collect_preferences", data: {}})
// this.$vuetify.theme.dark = localStorage.getItem('theme') == 'dark'
// Collect accounts from 'Accounts.db' by ruby sqlite3
sketchup.exec({name: "init_local_accounts", data: {}})
},
methods: {
accounts() {
return JSON.parse(localStorage.getItem('localAccounts'))
},
activeAccount(){
return this.accounts().find((account) => account['isDefault'])
},
switchAccount(account) {
this.$mixpanel.track('Connector Action', { name: 'Account Select' })
global.setSelectedAccount(account)
+13 -2
View File
@@ -1,6 +1,8 @@
<template>
<v-snackbar v-model="snack" app bottom color="primary">
{{ text }}
<v-snackbar v-if="text" v-model="snack" app bottom :color="color">
<div v-for="(line, index) in text.split('\n')" :key="index">
{{ line }}
</div>
<template #action="{}">
<v-btn v-if="actionName" small outlined @click="openUrl(url)" @click:append="snack = false">
{{ actionName }}
@@ -16,6 +18,7 @@ export default {
data() {
return {
snack: false,
color: "primary",
text: null,
actionName: null,
url: null
@@ -33,6 +36,14 @@ export default {
mounted() {
this.$eventHub.$on('notification', (args) => {
this.snack = true
this.color = "primary"
this.text = args.text
this.actionName = args.action ? args.action.name : null
this.url = args.action ? args.action.url : null
})
this.$eventHub.$on('error', (args) => {
this.snack = true
this.color = "#CC3300"
this.text = args.text
this.actionName = args.action ? args.action.name : null
this.url = args.action ? args.action.url : null
+57 -17
View File
@@ -5,7 +5,11 @@
@mouseenter="hover = true"
@mouseleave="hover = false"
>
<v-toolbar flat height="70">
<v-toolbar flat height="70" :color="getColor(invalid)">
<v-btn v-tooptip="''" icon small outlined class="delta-btn" v-if="invalid" @click="activateDiffing">
<v-icon v-if="!diffing" class="toggleUpDown" :class='{ "rotate": diffing }' small>mdi-eye-off-outline</v-icon>
<v-icon v-else class="toggleUpDown" :class='{ "rotate": diffing }' small>mdi-eye</v-icon>
</v-btn>
<v-toolbar-title class="ml-0" style="position: relative; left: -10px">
<!-- Uncomment when pinning is in place and add style="position: relative; left: -10px" to the element above :) -->
<v-btn
@@ -55,7 +59,7 @@
<timeago class="mr-1" :datetime="stream.updatedAt" />
|
<v-icon class="ml-1" small>mdi-account-key-outline</v-icon>
{{ stream.role.split(':')[1] }}
{{ stream.role === null ? "" : stream.role.split(':')[1] }}
</v-card-text>
<v-card-text class="d-flex align-center pb-5 mb-5 -mt-2" style="height: 50px">
<v-menu offset-y>
@@ -157,7 +161,6 @@ import gql from 'graphql-tag'
import { bus } from '../main'
import streamQuery from '../graphql/stream.gql'
import ObjectLoader from '@speckle/objectloader'
import { BaseObjectSerializer } from '../utils/serialization'
global.convertedFromSketchup = function (streamId, batches, commitId, totalChildrenCount) {
bus.$emit(`sketchup-objects-${streamId}`, batches, commitId, totalChildrenCount)
@@ -198,7 +201,9 @@ export default {
loadingStage: null,
branchName: 'main',
commitId: 'latest',
commitMessage: null
commitMessage: null,
invalid: false,
diffing: false
}
},
apollo: {
@@ -280,6 +285,17 @@ export default {
}
},
mounted() {
bus.$on(`deactivate-diffing-${this.streamId}`, () => {
this.diffing = false
})
bus.$on(`validate-stream-${this.streamId}`, () => {
this.invalid = false
console.log(`validate-stream-${this.streamId}`)
})
bus.$on(`invalidate-stream-${this.streamId}`, () => {
this.invalid = true
console.log(`invalidate-stream-${this.streamId}`)
})
bus.$on(`refresh-stream-${this.streamId}`, () => {
let oldBranchName = this.branchName
let oldCommitId = this.commitId
@@ -315,6 +331,13 @@ export default {
if (this.saved) sketchup.exec({name: "notify_connected", data: {stream_id: this.streamId}})
},
methods: {
getColor(invalid){
if(invalid){
return "#ffdfdf"
} else {
return ""
}
},
sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
},
@@ -331,6 +354,16 @@ export default {
this.$mixpanel.track('Connector Action', { name: 'Commit Switch' })
this.commitId = commitId
},
activateDiffing(){
if (this.diffing){
this.diffing = false
sketchup.exec({name: "deactivate_diffing", data: {}})
return
}
this.diffing = true
bus.$emit("deactivate-diffing-except", (this.streamId))
sketchup.exec({name: "activate_diffing", data: {stream_id: this.streamId}})
},
toggleSavedStream() {
if (this.saved) {
sketchup.exec({name: "remove_stream", data: {stream_id: this.streamId}})
@@ -365,7 +398,8 @@ export default {
stream_name: this.stream.name,
stream_id: this.streamId,
branch_name: this.selectedCommit.branchName,
branch_id: this.selectedCommit.id
branch_id: this.selectedCommit.id,
source_app: this.selectedCommit.sourceApplication
}})
await this.$apollo.mutate({
@@ -401,21 +435,11 @@ export default {
this.loadingSend = false
this.loadingStage = null
this.$eventHub.$emit('notification', {
text: 'No objects selected. Nothing was sent.'
text: 'No objects selected. Nothing was sent.\n'
})
return
}
// let s = new BaseObjectSerializer()
// let startTime = new Date()
// let { hash, serialized } = s.writeJson(objects)
// let endTime = new Date();
// let timeDiff = endTime - startTime; //in ms
// // strip the ms
// timeDiff /= 1000;
// // get seconds
// console.log(timeDiff + " seconds");
try {
this.loadingStage = 'uploading'
this.loadingSend = true
@@ -455,7 +479,7 @@ export default {
})
console.log('>>> SpeckleSketchUp: Sent to stream: ' + this.streamId, commit)
this.$eventHub.$emit('notification', {
text: 'Model selection sent!',
text: 'Model selection sent!\n',
action: {
name: 'View in Web',
url: `${localStorage.getItem('serverUrl')}/streams/${this.streamId}/commits/${
@@ -488,6 +512,22 @@ export default {
</script>
<style scoped>
.delta-btn.v-btn--outlined {
border: 1px solid #474747;
background: white;
margin-left: -90px;
margin-top: -50px;
position: absolute;
}
.toggleUpDown {
transition: transform .3s ease-in-out !important;
}
.toggleUpDown.rotate {
transform: rotate(180deg);
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.2s ease-in;
@@ -89,10 +89,7 @@ export default {
computed: {
loggedIn() {
return localStorage.getItem('SpeckleSketchup.AuthToken') !== null
},
accounts() {
return JSON.parse(localStorage.getItem('localAccounts'))
},
}
},
methods: {
async createBranch(){
@@ -114,7 +111,7 @@ export default {
this.showCreateBranch = false
this.branchName = ""
this.description = ""
this.$mixpanel.track('Connector Action', { name: 'Create Branch' })
this.$mixpanel.track('Connector Action', { name: 'Branch Create' })
return res
}
}
@@ -92,7 +92,6 @@
</v-dialog>
<!-- DIALOG: Add a Stream by ID or URL -->
<!--
<v-dialog v-model="showCreateStreamById">
<template #activator="{ on, attrs }">
<v-btn
@@ -141,14 +140,13 @@
:disabled="createStreamByIdText === ''"
color="blue darken-1"
text
@click="showCreateStreamById = false"
@click="getStream"
>
Add
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
-->
</v-col>
</v-row>
</v-container>
@@ -159,9 +157,20 @@
import gql from "graphql-tag";
import {bus} from "@/main";
import userQuery from "@/graphql/user.gql";
import {StreamWrapper} from "@/utils/streamWrapper";
export default {
name: "CreateStreamDialog",
props: {
accountId: {
type: String,
default: null
},
serverUrl: {
type: String,
default: null
}
},
data() {
return {
showCreateNewStream: false,
@@ -177,10 +186,7 @@ export default {
computed: {
loggedIn() {
return localStorage.getItem('SpeckleSketchup.AuthToken') !== null
},
accounts() {
return JSON.parse(localStorage.getItem('localAccounts'))
},
}
},
apollo: {
user: {
@@ -188,16 +194,71 @@ export default {
}
},
methods: {
activeAccount(){
return JSON.parse(localStorage.getItem('selectedAccount'))
},
switchAccountToCreateStream(account) {
this.accountToCreateStream = account
},
refresh() {
this.$apollo.queries.user.refetch()
bus.$emit('refresh-streams')
},
async getStream(){
try {
const streamWrapper = new StreamWrapper(this.createStreamByIdText, this.accountId, this.serverUrl)
let res = await this.$apollo.query({
query: gql`
query Stream($id: String!){
stream(id: $id){
id
name
description
isPublic
role
createdAt
updatedAt
commentCount
favoritedDate
favoritesCount
collaborators {
id
name
role
avatar
},
branches (limit: ${10}){
totalCount,
cursor,
items {
id,
name,
description,
commits {
totalCount
}
}
}
}
}
`,
variables: {
id: streamWrapper.streamId
}
})
let stream = res.data.stream
this.$eventHub.$emit('notification', {
text: 'Stream Added by URL!\n',
})
bus.$emit('stream-added-by-id-or-url', stream.id)
this.$mixpanel.track('Connector Action', { name: 'Stream Add From URL' })
}
catch (e){
this.$eventHub.$emit('error', {
text: 'The stream you are trying to add might;\n' +
'- lies on different server, \n' +
'- be private, \n' +
'- not be existed anymore.',
})
}
this.showCreateStreamById = false
},
async createStream(){
let res = await this.$apollo.mutate({
mutation: gql`
@@ -216,7 +277,7 @@ export default {
this.showCreateNewStream = false
this.streamName = ""
this.description = ""
this.$mixpanel.track('Connector Action', { name: 'Create Stream' })
this.$mixpanel.track('Connector Action', { name: 'Stream Create' })
sketchup.exec({name: "save_stream", data: {stream_id: res["data"]["streamCreate"]}})
this.refresh()
return res
+162 -51
View File
@@ -23,26 +23,75 @@
<v-icon>mdi-theme-light-dark</v-icon>
</v-btn>
<span>Color Mode</span>
<div class="sm1 mt-3">Send Strategy</div>
<v-divider class="mb-2"/>
<!-- Register objects as Speckle Entity on send/receive -->
<v-switch
v-model="combineFacesByMaterial"
class="pt-1 mt-n2 mb-n2"
:label="'Combine faces by material under mesh'"
/>
<v-switch
v-model="includeAttributes"
class="pt-1 my-n5"
:label="'Include entity attributes'"
/>
<div class="sm1 mt-3">Receive Strategy</div>
<v-divider class="mb-2"/>
<v-switch
v-model="mergeCoplanarFaces"
class="pt-1 mt-n2 mb-n2"
:label="'Merge co-planar faces on receive'"
:input-value="registerSpeckleEntity"
class="pt-3 mt-n2 mb-n7"
:label="'Register objects as Speckle Entity on send/receive'"
@change="registerSpeckleEntityHandler"
/>
<!-- Switch Diffing -->
<v-switch
:input-value="diffing"
class="pt-3 mt-n2 mb-n2"
:label="'Diffing (Alpha)'"
@change="diffingHandler"
/>
<div class="sm1 mt-3">Model Preferences</div>
<v-divider class="mb-2"/>
<v-switch
class="pt-1 mt-n2 mb-n2"
:input-value="combineFacesByMaterial"
:label="'Combine faces by material under mesh'"
@change="combineFacesByMaterialHandler"
/>
<v-switch
:input-value="includeAttributes"
class="pt-1 my-n5"
:label="'Include entity attributes'"
@change="includeAttributesHandler"
/>
<v-icon class="ml-3" style="line-height: 0;">mdi-arrow-right-bottom</v-icon>
<v-switch
:input-value="includeEdgeAttributes"
class="pt-1 my-n5 ml-10"
:label="'Edge'"
:disabled="!includeAttributes"
@change="includeEdgeAttributesHandler"
/>
<v-icon class="ml-3" style="line-height: 0;">mdi-arrow-right-bottom</v-icon>
<v-switch
:input-value="includeFaceAttributes"
class="pt-1 my-n5 ml-10"
:label="'Face'"
:disabled="!includeAttributes"
@change="includeFaceAttributesHandler"
/>
<v-icon class="ml-3" style="line-height: 0;">mdi-arrow-right-bottom</v-icon>
<v-switch
:input-value="includeGroupAttributes"
class="pt-1 my-n5 ml-10"
:label="'Group'"
:disabled="!includeAttributes"
@change="includeGroupAttributesHandler"
/>
<v-icon class="ml-3" style="line-height: 0;">mdi-arrow-right-bottom</v-icon>
<v-switch
:input-value="includeComponentAttributes"
class="pt-1 my-n5 ml-10"
:label="'Component'"
:disabled="!includeAttributes"
@change="includeComponentAttributesHandler"
/>
<v-switch
:input-value="mergeCoplanarFaces"
class="pt-1 mt-n2 mb-n2"
:label="'Merge co-planar faces on receive'"
@change="mergeCoplanarFacesHandler"
/>
</v-container>
<v-card-actions>
@@ -78,55 +127,117 @@ export default {
description: "",
combineFacesByMaterial: this.preferences.model.combine_faces_by_material,
includeAttributes: this.preferences.model.include_entity_attributes,
includeFaceAttributes: this.preferences.model.include_face_entity_attributes,
includeEdgeAttributes: this.preferences.model.include_edge_entity_attributes,
includeGroupAttributes: this.preferences.model.include_group_entity_attributes,
includeComponentAttributes: this.preferences.model.include_component_entity_attributes,
mergeCoplanarFaces: this.preferences.model.merge_coplanar_faces,
diffing: this.preferences.user.diffing,
registerSpeckleEntity: this.preferences.user.register_speckle_entity
}
},
watch: {
'preferences': {
handler(newValue) {
this.combineFacesByMaterial = newValue.model.combine_faces_by_material
this.includeAttributes = newValue.model.include_entity_attributes
this.includeFaceAttributes = newValue.model.include_face_entity_attributes
this.includeEdgeAttributes = newValue.model.include_edge_entity_attributes
this.includeGroupAttributes = newValue.model.include_group_entity_attributes
this.includeComponentAttributes = newValue.model.include_component_entity_attributes
this.mergeCoplanarFaces = newValue.model.merge_coplanar_faces
this.diffing = newValue.user.diffing
this.registerSpeckleEntity = newValue.user.register_speckle_entity
},
deep: true,
immediate: true
},
'showSettings': {
handler(newValue) {
if (newValue){
this.$mixpanel.track('Connector Action', { name: 'Open Settings Dialog' })
this.$mixpanel.track('Connector Action', { name: 'Settings Open' })
}
},
deep: true
},
'combineFacesByMaterial': {
handler(newValue) {
sketchup.exec({
name: "model_preferences_updated",
data: {preference: "combine_faces_by_material", value: newValue}
})
this.$mixpanel.track('Connector Action', { name: 'Combine Faces By Material Option' })
},
deep: true
},
'includeAttributes': {
handler(newValue) {
sketchup.exec({
name: "model_preferences_updated",
data: {preference: "include_entity_attributes", value: newValue}
})
this.$mixpanel.track('Connector Action', { name: 'Include Entity Attributes Option' })
},
deep: true
},
'mergeCoplanarFaces': {
handler(newValue) {
sketchup.exec({
name: "model_preferences_updated",
data: {preference: "merge_coplanar_faces", value: newValue}
})
this.$mixpanel.track('Connector Action', { name: 'Merge Co-Planar Faces Option' })
},
deep: true
}
}
},
methods: {
diffingHandler(newValue){
this.diffing = !!newValue
sketchup.exec({
name: "user_preferences_updated",
data: {preference_hash: "configSketchup", preference: "diffing", value: this.diffing}
})
this.$mixpanel.track('Connector Action', { name: 'Toggle Diffing' })
},
registerSpeckleEntityHandler(newValue){
this.registerSpeckleEntity = !!newValue
sketchup.exec({
name: "user_preferences_updated",
data: {preference_hash: "configSketchup", preference: "register_speckle_entity", value: this.registerSpeckleEntity}
})
this.$mixpanel.track('Connector Action', { name: 'Toggle Register Speckle Entity' })
},
combineFacesByMaterialHandler(newValue){
this.combineFacesByMaterial = !!newValue
sketchup.exec({
name: "model_preferences_updated",
data: {preference: "combine_faces_by_material", value: this.combineFacesByMaterial}
})
this.$mixpanel.track('Connector Action', { name: 'Toggle Combine Faces By Material' })
},
includeAttributesHandler(newValue){
this.includeAttributes = !!newValue
sketchup.exec({
name: "model_preferences_updated",
data: {preference: "include_entity_attributes", value: this.includeAttributes}
})
this.$mixpanel.track('Connector Action', { name: 'Toggle Include Entity Attributes' })
},
includeFaceAttributesHandler(newValue){
this.includeFaceAttributes = !!newValue
sketchup.exec({
name: "model_preferences_updated",
data: {preference: "include_face_entity_attributes", value: this.includeFaceAttributes}
})
this.$mixpanel.track('Connector Action', { name: 'Toggle Include Face Entity Attributes' })
},
includeEdgeAttributesHandler(newValue){
this.includeEdgeAttributes = !!newValue
sketchup.exec({
name: "model_preferences_updated",
data: {preference: "include_edge_entity_attributes", value: this.includeEdgeAttributes}
})
this.$mixpanel.track('Connector Action', { name: 'Toggle Include Edge Entity Attributes' })
},
includeGroupAttributesHandler(newValue){
this.includeGroupAttributes = !!newValue
sketchup.exec({
name: "model_preferences_updated",
data: {preference: "include_group_entity_attributes", value: this.includeGroupAttributes}
})
this.$mixpanel.track('Connector Action', { name: 'Toggle Include Group Entity Attributes' })
},
includeComponentAttributesHandler(newValue){
this.includeComponentAttributes = !!newValue
sketchup.exec({
name: "model_preferences_updated",
data: {preference: "include_component_entity_attributes", value: this.includeComponentAttributes}
})
this.$mixpanel.track('Connector Action', { name: 'Toggle Include Component Entity Attributes' })
},
mergeCoplanarFacesHandler(newValue){
this.mergeCoplanarFaces = !!newValue
sketchup.exec({
name: "model_preferences_updated",
data: {preference: "merge_coplanar_faces", value: this.mergeCoplanarFaces}
})
this.$mixpanel.track('Connector Action', { name: 'Toggle Merge Co-Planar Faces' })
},
switchTheme() {
this.$vuetify.theme.dark = !this.$vuetify.theme.dark
sketchup.exec({
name: "user_preferences_updated",
data: {preference_hash: "configSketchup", preference: "DarkTheme", value: this.$vuetify.theme.dark}
data: {preference_hash: "configSketchup", preference: "dark_theme", value: this.$vuetify.theme.dark}
})
this.$mixpanel.track('Connector Action', { name: 'Toggle Theme' })
},
+1
View File
@@ -42,6 +42,7 @@ query Stream($id: String!) {
branchName
authorName
authorAvatar
sourceApplication
referencedObject
}
}
+15 -4
View File
@@ -10,9 +10,12 @@ const SpeckleMetrics = {
Vue.prototype.$mixpanel = mixpanel
Vue.prototype.$mixpanel.init(token, config)
Vue.prototype.$mixpanel.register({ hostApp: 'sketchup', type: 'action' })
Vue.prototype.$refreshMixpanelIds = function () {
// 1. It logs out the user and removes out the registered props.
Vue.prototype.$mixpanel.reset()
// 2. create hashes for user distinction
let distinctId =
'@' +
crypto
@@ -29,12 +32,20 @@ const SpeckleMetrics = {
.digest('hex')
.toUpperCase()
// 3. Setting super properties that will be sent with every event
Vue.prototype.$mixpanel.register({
// eslint-disable-next-line camelcase
distinct_id: distinctId,
// eslint-disable-next-line camelcase
hostApp: 'sketchup',
type: 'action',
hostAppVersion: localStorage.getItem('hostAppVersion'),
core_version: localStorage.getItem('speckleVersion'),
server_id: serverId
})
// 4. Logging into user
Vue.prototype.$mixpanel.identify(distinctId)
// 5. If it is a registered user we can consider it is identified
Vue.prototype.$mixpanel.people.set("Identified", true)
}
}
}
+77
View File
@@ -0,0 +1,77 @@
require('url')
export class StreamWrapper {
constructor(streamIdOrUrl, accountId, serverUrl) {
this.originalOutput = streamIdOrUrl
try {
this.streamWrapperFromUrl(streamIdOrUrl)
}
catch (e){
this.serverUrl = serverUrl
this.userId = accountId
this.streamId = streamIdOrUrl
}
}
streamWrapperFromUrl(streamUrl){
this.url = new URL(streamUrl)
this.segments = this.url.pathname.split('/').map((segment) => segment + '/')
this.serverUrl = this.url.origin
if (this.segments.length >= 4 && this.segments[3]?.toLowerCase() === "branches/"){
this.streamId = this.segments[2].replace("/", "")
if (this.segments.length > 5)
{
let branchSegments = this.segments.slice(4, this.segments.length - 1);
this.branchName = branchSegments.join("")
}
else
{
this.branchName = this.segments[4]
}
} else {
switch (this.segments.length){
case 3: // ie http://speckle.server/streams/8fecc9aa6d
if (this.segments[1].toLowerCase() === "streams/")
this.streamId = this.segments[2].replace("/", "");
else
throw new Error(`Cannot parse ${this.originalOutput} into a stream wrapper class`);
break;
case 4: // ie https://speckle.server/streams/0c6ad366c4/globals/
if (this.segments[3].toLowerCase().startsWith("globals"))
{
this.streamId = this.segments[2].replace("/", "");
this.branchName = this.segments[3].replace("/", "");
}
else
throw new Error(`Cannot parse ${this.originalOutput} into a stream wrapper class`);
break;
case 5: // ie http://speckle.server/streams/8fecc9aa6d/commits/76a23d7179
switch (this.segments[3].toLowerCase()){
case "commits/":
this.streamId = this.segments[2].replace("/", "");
this.commitId = this.segments[4].replace("/", "");
break;
case "globals/":
this.streamId = this.segments[2].replace("/", "");
this.branchName = this.segments[3].replace("/", "");
this.commitId = this.segments[4].replace("/", "");
break;
case "branches/":
this.streamId = this.segments[2].replace("/", "");
this.branchName = this.segments[4].replace("/", "");
break;
case "objects/":
this.streamId = this.segments[2].replace("/", "");
this.objectId = this.segments[4].replace("/", "");
break;
default:
throw new Error(`Cannot parse ${this.originalOutput} into a stream wrapper class`);
}
break;
default:
throw new Error(`Cannot parse ${this.originalOutput} into a stream wrapper class`);
}
}
}
}
+39 -1
View File
@@ -44,6 +44,14 @@ global.setSavedStreams = function (streamIds) {
bus.$emit('set-saved-streams', streamIds)
}
global.updateInvalidStreams = function (streamIds) {
bus.$emit('validate-streams')
streamIds.forEach((id) => {
bus.$emit(`invalidate-stream-${id}`)
})
}
const streamLimit = 5
export default {
name: 'Streams',
@@ -72,15 +80,45 @@ export default {
}
},
mounted() {
bus.$on("deactivate-diffing-except", (exceptedStreamId) => {
this.savedStreams.forEach((streamId) => {
if (streamId !== exceptedStreamId){
bus.$emit(`deactivate-diffing-${streamId}`)
}
})
this.allStreamsList.forEach((stream) => {
if (stream.id !== exceptedStreamId){
bus.$emit(`deactivate-diffing-${stream.id}`)
}
})
})
bus.$on('validate-streams', () => {
this.savedStreams.forEach((streamId) => {
bus.$emit(`validate-stream-${streamId}`)
})
this.allStreamsList.forEach((stream) => {
bus.$emit(`validate-stream-${stream.id}`)
})
})
bus.$on('refresh-streams', () => {
// TODO: We should remember selected branches and commits before refetch
this.$apollo.queries.streams.refetch()
// TODO: We should set previously selected branches and commits after refetch
})
bus.$on('set-saved-streams', (streamIds) => {
this.savedStreams = streamIds
})
bus.$on('stream-added-by-id-or-url', (streamId) => {
if (!this.savedStreams){
this.savedStreams = []
this.savedStreams.push(streamId)
} else {
if (!this.savedStreams.includes(streamId)){
this.savedStreams.push(streamId)
}
}
sketchup.exec({name: "save_stream", data: {stream_id: streamId}})
})
sketchup.exec({name: "load_saved_streams", data: {}})
console.log('LAUNCHED')
this.$mixpanel.track('Connector Action', { name: 'Launched' })