Automatically load a component.
-
Hello, kaas
My "cube.skp" is in the "Components" folder in my plugin.
It should be placed in another folder?
You can download HERE the full fichier.rbz.
Thank you for your help.
cordially
David
Ps: Thank you for your links.
Edit: Small error in "TesteCube.rb" corrected.
-
@dan rathbun said:
David, code files need to be encoded as UTF-8 without BOM (BOM = Bit Order Mark.)
The first line of all SketchUp code files should also always be the "magic comment":
encoding: UTF-8
Another thing I just learned.
Have you sample code
# encoding: UTF-8
?I supose that my error is because of that ?
Thanks
-
It's a good idea to include that as the first line of all of your RB files.
But, it's not essential - if you use Notepad++ exe you can set up its preferences so that all new files you create are set to be properly encoded UTF-8 [without BOM] - which is required by the newer versions of Ruby.
Unfortunately the Windows bare-bones Notepad exe uses an inappropriate text encoding system - ANSI I think ?
And if you have an existing RB file that is encoded 'wrongly' [e.g. ANSI] you can use Notepad++ to change its encoding to UTF-8, then save it - and it should then be permanently set to that proper encoding...Get Notepad++ exe v7 from here - it's free and has helpful Ruby syntax tools built in...
https://notepad-plus-plus.org/download/v7.html
There are 32 and 64 bit versions - I use the 64... -
Thank you for the download link TIG.
I encoded my "rb" in "UTF-8", without BOM!
It is not the cause of the error message.
You can download HERE, the new "RBZ".
Thank you in advance for your help.
-
The error points you into the right direction:
Error: #<NameError: undefined local variable or method
entities' for TNTDAVID::ClickWindow3D_Trial_mm:Module>`My guess is, the error refers to:
instance = **entities**.add_instance(component_definition, transformation)
If you change that line to:
instance = model.entities.add_instance(component_definition, transformation)
you might have more luck.
-
@Kaas is correct.
Try...
entities = model.active_entities instance = entities.add_instance(component_definition, transformation)
There have been so many iterations of your code fragments across two threads it becomes hard to keep track !
-
It works
Many Thanks, TIG, kaas and Dan Rathbun.
Anyone can download HERE, the "RBZ".
Here is the magic code:
module TesteCube module CubeImport def self.auto_run_code() path = Sketchup.find_support_file("cube.skp", "Plugins/TesteCube/Components/") model = Sketchup.active_model definitions = model.definitions component_definition = definitions.load(path) point = Geom;;Point3d.new(10, 20, 30) transformation = Geom;;Transformation.new(point) instance = model.entities.add_instance(component_definition, transformation) end self.auto_run_code() end end
We just reach the first goal!
There remain two to accomplish:
**1.How to run the code on each new scenes?**
Currently only the launch SketchUp, triggers the code that import the "cube.skp".
**2. How to lock or hide the cube, to avoid copying or unintended deletions?**
More the cube is protected, more "Dynamic Components" related also.
Thank you for your Help.
Cordially
David
-
To lock my "[b]cube.stp", you just add this line:[/b]
status = instance.locked = true #### Locks the cube. layers = model.layers new_layer = layers.add "cube" #### Create a new layer "cube". new_layer.visible = false #### Hides the "cube" layer.
I can not find how to add the new layer on my cube!
You have an idea ?
Thank you
-
That's the easy bit !
instance.layer = new_layer
You could also lock it, so that its harder to delete if someone activates that layer later
instance.locked = true
You can also hide it for good measure, so it's harder to find if !
instance.hidden = true
However, nothing is foolproof...
Layers > 'Cube' > ON shows it...
View > Hidden Geometry > ON shows it...
Select instance and right-click > Unlock > Unlocks it...
Select instance and right-click > Unhide > Shows fully it...
Hit delete > to remove it...
or Open Component Browser find 'Cube' and use context-menu to delete its definition, when prompted accept...
or Layers > Delete 'Cube' and accept to delete all entities using it... -
Thank you TIG it works perfectly!
The most important is to prevent the accidental suppresion.
Same CleanUp does not remove my cube.
Protection against copying is not good.
Is there a solution to make my cube inaccecible from SketchUp?
For example, do not display the "cube" layer in the Layers window?
%(#FF0000)[**
It still remains a very important objective to accomplish:**]
How do I load my "Plugin" has every opening a SketchUp file?
I doubt that it will be harder to find!
Thank you in advance for your help.
David
-
Hello everyone.
As my last objective seems difficult, I return to my first idea.
Instead of automatically import the "cube.skp" on each new SketchUp scene.
The "cube" will be imported along with the first "Dynamic Component".
Here is the modified code and that works:
module TesteCube module CubeImport path_to_components = File.join(File.dirname(__FILE__), 'Components') @@path_to_00 = File.join(path_to_components, 'Dynamic Component.skp') @@path_to_resources = File.join(File.dirname(__FILE__), 'Resources') def self.methode1 path = Sketchup.find_support_file("cube.skp", "Plugins/TesteCube/Components/") model = Sketchup.active_model definitions = model.definitions component_definition = definitions.load(path) point = Geom;;Point3d.new(0, 0, 0) transformation = Geom;;Transformation.new(point) instance = model.entities.add_instance(component_definition, transformation) status = instance.locked = true layers = model.layers new_layer = layers.add "cube" new_layer.visible = false instance.layer = new_layer instance.locked = true instance.hidden = true Sketchup.active_model.import(@@path_to_00) end if !file_loaded?(__FILE__) su_menu = UI.menu("Plugins") cc_menu = su_menu.add_submenu(%Q(Teste_Cube)) tb = UI;;Toolbar.new(%Q(Teste-Cube)) command1 = UI;;Command.new("add Dynamic Component"){ CubeImport.methode1 } command1.small_icon = File.join(@@path_to_resources,"add_00.png") command1.large_icon = File.join(@@path_to_resources,"add_00.png") command1.tooltip = %Q(add Dynamic Component) command1.status_bar_text = %Q(add Dynamic Component) command1.menu_text = %Q(add Dynamic Component) cc_menu.add_item(command1) tb.add_item(command1) tb.restore file_loaded(__FILE__) end end end
Notice that "method1" to one more line to import "Dynamique. Component.skp"
at the same time that the "cube.skp".The line of code with the more interest to me is:
command1 = UI::Command.new("add cube") { CubeImport.methode1 }
She created a new icon { that triggers methode1 }.
If I click once on the icon:
methode1 will import "1 cube" + "1 component Dynamic".
If I click ten times on the icon:
method1 will import "10 cubes" + "10 Dynamic Components".
That's exactly my problem
I do need a single "cube", with no restrictions on the number of "Dynamic Components".
How to limit the amount of cube imported at "1" ?
Through links provided by DanRathbun and especially "Ruby in twenty minutes",
I understand better now the ruby.But this is not sufficient to finalize my code as a great.
Thank you in advance for your help.
Cordially
David
-
To limit the amount of cube to import, I found this method:
ComponentInstance.manifold?
This is the method that can meet my expectations?
I tried many things and it does not work:
instance.componentinstance.manifold instance = false
componentinstance.manifold instance = false
instance = componentinstance.manifold? instance.componentinstance.manifold = false
status = istance.componentinstance.manifold? status.componentinstance.manifold = false
componentinstance.manifold status = false
instance.componentinstance.manifold status = false
instance.componentinstance.manifold = false
Nothing works !
And all methods that are following this method are not executed.
I need your help more than ever.
Thank you
David
-
Manifold is used for testing if its a solid or not. Not the right tool for your problem.
So you just need 1 cube in the model: you have linked the 'cube' to 'path' and you refer to it as 'component_definition'. You insert the cube using:
instance = model.entities.add_instance(component_definition, transformation)
Why not create a small test and see if an instance is already in the model and if so - don't load it again. Replace the above line with these lines:
in_scene=false definitions.each{|d| if d.name == 'cube' c = d.count_used_instances in_scene = true if c > 0 end } if !in_scene instance = model.entities.add_instance(component_definition, transformation) end
-
You seem to be misunderstanding how some things [many things!] work.
!!! .manifold? is NOT what you need it's for testing if a group or instance is a a 'solid' - e.g. for 3d printing.When your extension starts to load the FIRST DC it initially adds 'definition'.
AND it then adds an 'instance' of that.
Subsequent DC loads should also check in case the definition or instance are not present...if definition.valid? if instance.valid? ###[or perhaps || definition.instances[0] ???] ### You are already set to do the DC load... else ### You need to add an instance - perhaps it's been deleted by the user ??? ### NOW you are set to do the DC load... end else ### definition is NOT valid - it's been deleted - so the instance has too. ### You load the definition AND add the instance again - seems like the user has deleted both ! ### NOW you are set to do the DC load... end
A certain logic is needed in thinking through the coded steps...
-
Following @Kaas's useful input...
I am unclear as to whether you want one 'cube' instance in each model or in each scene or in each entities-context ???
If you only ever need one then it's easily done, otherwise it gets more convoluted [but still do-able]... -
@kaas said:
Pourquoi ne pas créer un petit test et voir si une instance est déjà dans le modÚle et si oui - ne pas le charger à nouveau. Remplacer la ligne ci - dessus avec ces lignes:
CODE PROMO: SĂLECTIONNER TOUT
in_scene=false
definitions.each{|d|
  if d.name == 'cube'
    c = d.count_used_instances
    in_scene = true if c > 0
  end
}
if !in_scene
  instance = model.entities.add_instance(component_definition, transformation)
end
Like this?
module TesteCube module CubeImport path_to_components = File.join(File.dirname(__FILE__), 'Components') @@path_to_00 = File.join(path_to_components, 'Dynamic Component.skp') @@path_to_resources = File.join(File.dirname(__FILE__), 'Resources') def self.methode1 path = Sketchup.find_support_file("cube.skp", "Plugins/TesteCube/Components/") model = Sketchup.active_model definitions = model.definitions component_definition = definitions.load(path) point = Geom;;Point3d.new(0, 0, 0) transformation = Geom;;Transformation.new(point) in_scene=false definitions.each{|d| if d.name == 'cube' c = d.count_used_instances in_scene = true if c > 0 end } if !in_scene instance = model.entities.add_instance(component_definition, transformation) end status = instance.locked = true layers = model.layers new_layer = layers.add "cube" new_layer.visible = false instance.layer = new_layer instance.locked = true instance.hidden = true Sketchup.active_model.import(@@path_to_00) end if !file_loaded?(__FILE__) su_menu = UI.menu("Plugins") cc_menu = su_menu.add_submenu(%Q(Teste_Cube)) tb = UI;;Toolbar.new(%Q(Teste-Cube)) command1 = UI;;Command.new("add Dynamic Component"){ CubeImport.methode1 } command1.small_icon = File.join(@@path_to_resources,"add_00.png") command1.large_icon = File.join(@@path_to_resources,"add_00.png") command1.tooltip = %Q(add Sliding Window) command1.status_bar_text = %Q(add Sliding Window) command1.menu_text = %Q(add Sliding Window) cc_menu.add_item(command1) tb.add_item(command1) tb.restore file_loaded(__FILE__) end end end
When I click on my icon, nothing happens.
I made a mistake ?
@tig said:
I am unclear as to whether you want one 'cube' instance in each model or in each scene or in each entities-context ???
I want one cube for each SketchUp scene!
Thank you for your help.
-
Maybe this concept will more clear !
The mysterious thing for me is how to avoid the list of components (even hidden)
on the Onliner ?Bon courage!
-
My response is why use a hidden geometric construct to "protect" other components ?
I would instead use anAttributeDictionary
. -
@tntdavid said:
...When I click on my icon, nothing happens....
Not sure. Maybe you already have an instance of the component present in your model? Its easy to find out by debugging. Add just one line in this loop:
definitions.each{|d| if d.name == 'cube' c = d.count_used_instances puts %(there are #{c} instances in the model of cube) in_scene = true if c > 0 end }
This code won't help you though if you want to have just 1 cube per scene. That would make the code more complex.
Edit: Dan's suggestion might be just what you need. Its the only way to really hide data.
-
Pilou thank you for your clearer picture that my writings.
Because of my English very bad, I think I lacked clarity.
I want a single cube by Sketchup file.
In Sketchup file, you can have a complete project with many dynamic components, but you have a single cube!
@kaas said:
Not sure. Maybe you already have an instance of the component present in your model? Its easy to find out by debugging. Add just one line in this loop:
No instance is present in my file.
To test the code, you can download the extension HERE.
@kaas said:
This code won't help you though if you want to have just 1 cube per scene. That would make the code more complex.
It's complicated to automatically remove a "cube", when there are 2?
@dan rathbun said:
My response is why use a hidden geometric construct to "protect" other components ?
I would instead use an AttributeDictionary.Thank you for this information, I must annalyser this "class" with attention.
With the methods provided by this class, is it possible to make a dynamic component unusable without the presence of the extension?
For example :
If someone shares one of my dynamic components with a user without my extension, it must be impossible to use these dynamic features!
I want to go at the end of my research, because my final interest is not only the security of my dynamic components.
Thank you for your support
David
Advertisement