Class: Sketchup::Procedure Abstract
- Inherits:
-
Object
- Object
- Sketchup::Procedure
Overview
Implement the methods described in this class to create a procedure. Your subclass must call super from its #initialize, passing in the necessary arguments.
To create a new procedure in Ruby, you must define a new class that implements its methods.
Register the instance of your procedure class via register_procedure.
Example:
class MyProcedure < Sketchup::Procedure
ID = 'my_company.my_extension.my_proc_id'
DESC = 'My procedure description.'
VERSION = 1
def initialize
super(id: ID, description: DESC, version: VERSION)
end
def validate(args)
# Return true if valid, or an array of error hashes.
true
end
def run(args)
# Return true on success, or an array of error hashes.
end
def (args)
# Return a hash mapping widget IDs to their desired enabled/visible states.
{}
end
end
my_proc = MyProcedure.new
Sketchup.register_procedure(my_proc)
Instance Method Summary # collapse
-
#declare_params_and_ui(args) ⇒ String
abstract
A JSON string describing the parameters and UI.
- #draw(args) ⇒ nil abstract
-
#get_extents(args) ⇒ Geom::BoundingBox
abstract
The bounds of the preview drawn by #draw.
-
#get_widget_states(args) ⇒ Hash{String => Hash}
abstract
A hash mapping widget IDs to their desired states.
-
#initialize(id:, description:, version:) ⇒ Object
constructor
All arguments are required.
-
#on_button_clicked(args) ⇒ Symbol?
abstract
Return
:reload_uito request a full rebuild of the procedure’s UI (for example when the handler changed parameter values that the widgets need to reflect). - #on_open_close(args) ⇒ nil abstract
-
#run(args) ⇒ Boolean, Array<Hash>
abstract
You should first clear the output entities.
-
#validate(args) ⇒ Boolean, Array<Hash>
abstract
trueif valid,falseif invalid.
Constructor Details
↑ #initialize(id:, description:, version:) ⇒ Object
All arguments are required.
Instance Method Details
↑ #declare_params_and_ui(args) ⇒ String
Called by SketchUp to get the parameter and UI declaration for this procedure.
Returns A JSON string describing the parameters and UI. The keys in the JSON object are parameter identifiers (e.g., “count”, “spacing”) that match the parameter keys used when calling ComponentDefinition#attach_procedure. Each parameter identifier maps to an object containing UI widget specifications. See the Declarative UI Schema file for the full widget schema.
↑ #draw(args) ⇒ nil
(Optional) Called once per view refresh while the procedural component is open for editing, allowing the procedure to draw custom feedback into the view.
Since this method is called during drawing, it should run as fast as possible. Avoid slow computations and prefer simplified representations of the final geometry when possible.
↑ #get_extents(args) ⇒ Geom::BoundingBox
(Optional) Called while the procedural component is open for editing to get the bounds of what #draw renders. Implement this if the preview can extend beyond the bounds of the component, which could otherwise leave it clipped out of the view.
Since this method is called during drawing, it should run as fast as possible. It is strictly observational and it must not draw or modify the model.
Returns The bounds of the preview drawn by #draw. An empty bounding box means the preview needs no extra room.
↑ #get_widget_states(args) ⇒ Hash{String => Hash}
Called by SketchUp after the procedure UI is populated and after every parameter change to determine the enabled/visible state of each widget. A procedure does not need to implement this method if the widget states do not change.
Returns A hash mapping widget IDs to their desired states. Each state hash may contain :enabled and/or :visible boolean keys. Widget IDs not present in the returned hash keep their current state. See the Declarative UI Schema file for details on dynamic widget states.
↑ #on_button_clicked(args) ⇒ Symbol?
(Optional) Called when the user clicks a button widget declared in this procedure’s UI (a widget with “widgetType”: “button”). The procedure may perform arbitrary work against the model in response. A procedure does not need to implement this method if it does not declare any buttons.
If the handler modifies the model it should wrap the changes in its own operation using Model#start_operation and Model#commit_operation.
Return :reload_ui to request a full rebuild of the procedure’s UI (for example when the handler changed parameter values that the widgets need to reflect). Return nil or any other value to leave the existing widgets in place.
↑ #on_open_close(args) ⇒ nil
(Optional) Called when this procedure’s component is opened or closed for editing. The :action argument distinguishes the two. This can be useful if the procedure needs to activate a custom tool when its component is opened, for instance.
This method is strictly observational. Do not modify the model from within it and do not call Model#start_operation / Model#commit_operation. It can be called while undoing or redoing an open/close and always runs inside the open/close operation.
↑ #run(args) ⇒ Boolean, Array<Hash>
Called by SketchUp to execute the procedure.
You should first clear the output entities. Then validate the control geometry and parameters (you can call your own #validate method for this). If the input is invalid, return errors as appropriate. And finally generate the output geometry.
This method will be called with an operation already started by the framework. Do not explicitly call Model#start_operation and Model#commit_operation from within this code.
↑ #validate(args) ⇒ Boolean, Array<Hash>
Called by SketchUp to check if the given input entities can form a valid procedural component.
Do not make model modifications within this method. Also do not call Model#start_operation and Model#commit_operation.
Returns true if valid, false if invalid. Optionally, return an Array of error hashes to report specific validation errors. Each error hash may contain:
-
:title[String] A short title for the error. -
:description[String] Description of the error. -
:involved_entities[+Array<Integer>+] (optional) Persistent IDs (Entity#persistent_id) of the entities to be highlighted.