Skip to content

Commit

Permalink
~ Improved ComponentDefinition and Material uuids management (uses #p…
Browse files Browse the repository at this point in the history
…ersistent_id if SU > 20.1)
  • Loading branch information
bbeaulant committed Oct 15, 2020
1 parent 268c99a commit 9ce6d94
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ in 1.x and 0.x versions.
* Fixed Ruby 2.7.0 deprecated calls
* Fixed Layer0 visibility detection
* Fixed Dimension regex for *X' X X/X'* input format
* Improved ComponentDefinition and Material uuids management (uses #persistent_id if SU > 20.1)

* 1.9.5 (2020-10-01)

Expand Down
Binary file modified dist/ladb_opencutlist-dev.rbz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/manifest-dev.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.9.6-dev",
"build": "202010140858",
"build": "202010150726",
"url": "https://raw.githubusercontent.com/lairdubois/lairdubois-opencutlist-sketchup-extension/master/dist/ladb_opencutlist-dev.rbz"
}
2 changes: 1 addition & 1 deletion src/ladb_opencutlist/ruby/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module Ladb::OpenCutList

EXTENSION_NAME = 'OpenCutList'.freeze
EXTENSION_VERSION = '1.9.6-dev'.freeze
EXTENSION_BUILD = '202010140858'.freeze
EXTENSION_BUILD = '202010150726'.freeze

end
33 changes: 21 additions & 12 deletions src/ladb_opencutlist/ruby/model/attributes/definition_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,39 @@ def read_from_attributes(force_unique_uuid = false)
if @definition

# Special case for UUID that must be truely unique in the session
uuid = Plugin.instance.get_attribute(@definition, 'uuid', nil)
uuid = @definition.get_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'uuid', nil)
if uuid.nil? or (force_unique_uuid and @@used_uuids.include?(uuid))

# Generate a new UUID
uuid = SecureRandom.uuid
if Sketchup.version_number >= 2010000000

# Store the new uuid to definition attributes
@definition.set_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'uuid', uuid)
# Running on > SU20.1.0 Use ComponentDefinition#persistent_id
uuid = @definition.persistent_id

else

# Generate a new UUID
uuid = SecureRandom.uuid

# Store the new uuid to definition attributes
@definition.set_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'uuid', uuid)

end

end
@@used_uuids.push(uuid)
@uuid = uuid

begin
@numbers = JSON.parse(Plugin.instance.get_attribute(@definition, 'numbers', '{}'))
@numbers = JSON.parse(@definition.get_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'numbers', '{}'))
rescue JSON::ParserError
@numbers = {}
end
@cumulable = Plugin.instance.get_attribute(@definition, 'cumulable', CUMULABLE_NONE)
@orientation_locked_on_axis = Plugin.instance.get_attribute(@definition, 'orientation_locked_on_axis', false)
@labels = DefinitionAttributes.valid_labels(Plugin.instance.get_attribute(@definition, 'labels', []))
@length_increase = Plugin.instance.get_attribute(@definition, 'length_increase', '0')
@width_increase = Plugin.instance.get_attribute(@definition, 'width_increase', '0')
@thickness_increase = Plugin.instance.get_attribute(@definition, 'thickness_increase', '0')
@cumulable = @definition.get_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'cumulable', CUMULABLE_NONE)
@orientation_locked_on_axis = @definition.get_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'orientation_locked_on_axis', false)
@labels = DefinitionAttributes.valid_labels(@definition.get_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'labels', []))
@length_increase = @definition.get_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'length_increase', '0')
@width_increase = @definition.get_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'width_increase', '0')
@thickness_increase = @definition.get_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'thickness_increase', '0')
end
end

Expand Down
27 changes: 22 additions & 5 deletions src/ladb_opencutlist/ruby/model/attributes/material_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ def self.reset_used_uuids
@@used_uuids.clear
end

def self.write_persistent_id_to_uuid(material)
unless Sketchup.version_number < 2010000000 ||
material.get_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'uuid') ||
material.get_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'type').nil?
material.set_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'uuid', material.persistent_id)
end
end

def self.valid_type(type)
if type
i_type = type.to_i
Expand Down Expand Up @@ -324,14 +332,23 @@ def read_from_attributes(force_unique_uuid = false)
if @material

# Special case for UUID that must be truely unique in the session
uuid = Plugin.instance.get_attribute(@material, 'uuid', nil)
uuid = @material.get_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'uuid', nil)
if uuid.nil? or (force_unique_uuid and @@used_uuids.include?(uuid))

# Generate a new UUID
uuid = SecureRandom.uuid
if Sketchup.version_number >= 2010000000

# Store the new uuid to material attributes
@material.set_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'uuid', uuid)
# Running on > SU20.1.0 Use Material#persistent_id
uuid = @material.persistent_id

else

# Generate a new UUID
uuid = SecureRandom.uuid

# Store the new uuid to material attributes
@material.set_attribute(Plugin::ATTRIBUTE_DICTIONARY, 'uuid', uuid)

end

end
@@used_uuids.push(uuid)
Expand Down
9 changes: 3 additions & 6 deletions src/ladb_opencutlist/ruby/observer/app_observer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Ladb::OpenCutList

require 'singleton'
require_relative 'model_observer'
require_relative 'options_provider_observer'
require_relative 'materials_observer'
require_relative 'selection_observer'
Expand Down Expand Up @@ -43,9 +44,7 @@ def onActivateModel(model)

def add_model_observers(model)
if model
# if model.definitions
# model.definitions.add_observer(@definitions_observer)
# end
model.add_observer(ModelObserver.instance)
if model.options['UnitsOptions']
model.options['UnitsOptions'].add_observer(OptionsProviderObserver.instance)
end
Expand All @@ -60,9 +59,7 @@ def add_model_observers(model)

def remove_model_observers(model)
if model
# if model.definitions
# model.definitions.remove_observer(@definitions_observer)
# end
model.remove_observer(ModelObserver.instance)
if model.options['UnitsOptions']
model.options['UnitsOptions'].remove_observer(OptionsProviderObserver.instance)
end
Expand Down
25 changes: 25 additions & 0 deletions src/ladb_opencutlist/ruby/observer/model_observer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Ladb::OpenCutList

require 'singleton'

class ModelObserver < Sketchup::ModelObserver

include Singleton

def onPreSaveModel(model)
# puts "onPreSaveModel: #{model}"

if Sketchup.version_number > 2010000000

# Persists material persistent_ids to uuids
model.materials.each { |material|
MaterialAttributes.write_persistent_id_to_uuid(material)
}

end

end

end

end

0 comments on commit 9ce6d94

Please sign in to comment.