(Notice that I changed the constant reference "Component" to the local variable reference "component" !)
Also, it is better forum code etiquette if readers do not need to scroll horizontally to read your code. So please wrap argument lists in method calls like:
Also instance#make_unique does not return Boolean (true or false).
It returns a reference to a component instance. But the docs are unclear whether it is implemented the same as group#make_unique.
definition = defs.load(path)
before = definition.instances
# place the instances
placed = definition.instances - before # array subtraction
OR… you also can save references to each instance as you add them to the entities …
placed = []
# loop where instances are placed according to points:
points = [[0,0,0],[1,2,3],[3,4,5]]
for point in points
transformation = Geom::Transformation.new(point)
placed << entities.add_instance(definition, transformation)
end
the user may also want to fine tune it’s position, before interacting with it…
change the path and try this…
mod = Sketchup.active_model
defs = mod.definitions
path = '/Users/johns_iMac/Library/Application Support/SketchUp 2018/SketchUp/Components/tests/cube2.skp'
import = defs.load(path)
mod.place_component(import)
# after placing the comp the user may want to manipulate it using these tools
modifiers = ['ComponentTool', 'MoveTool', 'ScaleTool', 'RotateTool', 'OrbitTool', 'DollyTool' ]
names = Regexp.union(modifiers)
# this is just for reporting
@tool = ''
# we start a timer
what_comes_next = UI.start_timer(1.0, true) do
# that waits till the user tries to do soemthing else
if Sketchup.active_model.tools.active_tool_name =~ names
#
tool = Sketchup.active_model.tools.active_tool_name
p tool if tool != @tool
@tool = tool
else # this is before the 'pop' back
defn = import
ins = defn.instances
len = ins.length.freeze
if len > 1
ins.each do|i|
i.make_unique
end
end
# we can activate interact here if we want
Sketchup.active_model.select_tool(DCInteractTool.new($dc_observers))
p 'rename done'
UI.stop_timer(what_comes_next)
end # if tool
end # what_comes_next
# just use a single modifier
modifier = ['ComponentTool']
#and comment out the DC tool latter in the code
# Sketchup.active_model.select_tool(DCInteractTool.new($dc_observers))
You misunderstand. I did not (and will not) give complete solutions for development of a paid extension. Meaning, I will NOT do YOUR work FOR you.
I was giving examples that related to basic coding principles having to do with what I thought you wanted. I was hoping you will take some ideas and figure out the solution YOURSELF. This is the only way you will learn programming.
But your questions are not clear! It took you until the 7th post in the thread to finally state what you wanted …
The interactive mouse placing methods of the API, do NOT stop and wait. (This has already been discussed here in the forum. Search!)
To work with instances after placing, … you most likely need to implement a DefinitionObserver with a onComponentInstanceAdded callback method. You’ll need to attach the observer just before calling the place code, and detach it as soon as it’s no longer needed.
Do a forum search on “place_component” you’ll find some examples.
I’m thinking this risks targeting a nested group or component. Can we be sure the top level placed component is actually the last in the definition list?
So you want to make unique each component instance placed by the user, for specific component definitions?
The first thing that comes up to me is using an observer on each definition you wish to have this behaviour on.
# This is an example of an observer that watches a specific definition
# for new component insertions.
class MyDefObserver < Sketchup::DefinitionObserver
def onComponentInstanceAdded(definition, instance)
puts "onComponentInstanceAdded(#{definition}, #{instance})"
end
end
# Attach the observer
Sketchup.active_model.definitions[0].add_observer(MyDefObserver.new)
So, in the callback you could make the new instance unique. However, by doing so, you are generating a new component definiion, which you probably also want to attach this very same observer to.