Hello everyone, I wrote a small file Api Ruby, with which I create a pillar, setting the two
measurements at the base of the pillar, side along the x-axis and y and the height, all by entering the measurements
inputbox… in creating the face for the “pushpull”, the first point equals pt1 [0, 0, 0]
origin of the X, Y, Z axes … I would like the pillar object to remain momentarily attached
cursor and subsequently inserted on screen at a precise point at the click of the mouse insertion,
I hope I have explained myself… Possibly? What should I write to the Ruby file, for this insertion wait
pillar object… Thank you.
Thanks for the answer, I’ve already tried watching " model.place.component",
but I understood very little, if he was kind enough to show me with practical example
using my ruby file, it would be great…Thank you
Greetings Thomas. I feel like a fan talking to my idol here.
Small follow up question to place_component method.
It returns Sketchup::Model instead of the ComponentInstance. Seems reasonable since repeat argument could be set to true and it would return something like an array OR ComponentInstance.
Therefore, how would you grab a handle to the ComponentInstance (assuming repeat set to false).
Should I go with Sketchup.active_model.entities[-1] or is there something more intuitive.
1. Since you must have a reference to the component definition, you can take an array snapshot of it’s instances collection before and then subtract it from the array afterward.
before = cdef.instances
# Wait some time because place_component returns immediately.
new_instances = cdef.instances - before
But the main problem is knowing when the placing has been completed (as place_component is non-blocking and returns immediately.)
This is the reason why it just returns the model object - ie the method call returns immediately and does not wait for the user to place any instances.
Changing how it works now is not possible. But adding a sister method that does wait and retrun an array of placed instances is an option.
A better approach is likely to use a temporary definition observer …
# inside your plugin module ...
def onComponentInstanceAdded(definition, instance)
@instance = instance
@instance = nil
# Set up a timer to check for the new instance and remove observer
# if the timeout is reached (ie, user canceled the placement.)
@timeout = 10.0
@elapsed = 0
@tid = UI.start_timer(1.0,true) do
@elapsed += 1.0
elsif @elapsed > @timeout
# Attach this module as temporary observer:
# Place the instance with the MoveTool:
# Do something with @instance
end # plugin module
You might also watch the toolstack. The PlaceComponent tool will change to the MoveTool at some point.
There are a bunch of open API Issues regarding placing components with the API(s) …
how can I put the component when my code create the test-box at the point I want to,and it can be catch the mid-point or endpoint. at the same time, we can see the icon like this
I have searched many examples of similar problems in the forum，but there’s no completed code。can u help me finish the code then i can understand the methods to put components.Thks again！
Note that the forums is not usually the place for “completed code”.
(I myself have posted a smattering of complete code examples here, but intend to move them to GitHub gists or full code repositories.)
I did post a complete example Instance Drag Tool here …
Please learn Ruby best practices for a shared environment. ( ...click to expand... )
Regarding your “test” code. Please learn Ruby best practices for a shared environment. No global variables.
A unique namespace module wrapping everything (but even a name like “Testing” for namespace sake is fine for posting snippets.)
A namespace module is important because it is a no-no to define classes at the top level for your own use. (Only Ruby Core and API classes that everyone will use should be defined in the top level ObjectSpace.)
Within your top level namespace module is where you define extension submodules. This is important because each extension’s code needs to be separated from each other so that they do not interfere.
Define custom classes inside extension submodules where they will be used.
When coding use good practice so that when you need help others are able to understand what your code is trying to do:
Comment your classes and methods with at least a simple description. This lets other readers know what a class or method is intended to do. And later when you yourself come back to the code months later, these comments will help you remember how things work.
Insert comments in between code statements when more explanation is needed.
Use descriptive reference (variable) names, except for the simplest iterator index variables in loops.
Use proper 2 space indentation. (Set your code editor to replace tabs with spaces so that when you need to post code snippets the forum can display the code correctly.)
Thank a lot,Dan!
I have solved the problem ，i find the answer in official examples with writing notes myself。
As a beginner without coding experience, i think it’s too bad that there’s no notes in official examples’ code,many people can’t understand every method in code and why coding like this.people should search the API document and try to guess the way for complete codes。
Cos English is not my mother language，it’s too slow to learn skp develop。
fortunately，Dan help many beginner persevere in his study^_^