SketchUp二次开发之鼠标工具

    技术2025-02-15  17

    微信公众号:老顽童与小东邪 关注可了解更多的教程及排版技巧。问题或建议,请公众号留言; 如果你觉得对你有帮助,欢迎赞赏

    SketchUp二次开发之鼠标工具

    在SketchUp中,运用最多的就是鼠标事件,例如工具栏中的直线工具、矩形工具、圆弧工具等操作,都离不开鼠标事件。

    在日常生活中我们常用鼠标事件有,单击左键,双击左键,右键,中键等。当然在Tool对象中也提供了这些方法。

    onLButtonDown-按下鼠标左键

    onLButttonUp-释放鼠标左键

    onLButtonDoubleClick-双击鼠标左键

    onRButtonDown-按下鼠标右键

    onRButtonUp-释放鼠标右键

    onMouseMove-移动鼠标

    其中使用频率较高的有onLButtonDown、onLButttonUp、onRButtonDown、onRButtonUp、onMouseMove等方法。

    onMouseMove方法

    每当我们移动鼠标时,SketchUp就会调用onMouseMove方法。 Examples:

    def onMouseMove(flags, x, y, view) puts "onMouseMove: flags = #{flags}" puts " x = #{x}" puts " y = #{y}" puts " view = #{view}" end

    参数:

    flags (Integer) — 记录鼠标状态

    x (Integer) — 鼠标事件发生时在屏幕的X坐标

    y (Integer) — 鼠标事件发生时在屏幕的Y坐标

    view (Sketchup::View) — 视图

    onLButtonDown方法

    当我们按下鼠标左键时,SketchUp就会调用onLButtonDown方法。

    Examples:

    def onLButtonDown(flags, x, y, view) puts "onLButtonDown: flags = #{flags}" puts " x = #{x}" puts " y = #{y}" puts " view = #{view}" end

    参数意义与上文相近。在SketchUp中大多数工具中都会使用到这个方法。

    是不是觉得很抽象 下面我们结合例子来理解

    铅笔工具

    大家跟着我来实现一个简单的铅笔工具吧 我们先来回顾下上一章节的lwt_load.rb文件内容

    加载代码

    path = File.dirname(__FILE__).force_encoding('utf-8') files = [ File.join(path, 'lwt_plug/sphere.rb') ] files.each{|file| next unless File.file?(file) load file } # 新增工具条 menu = UI.menu('Extensions') toolbar = UI::Toolbar.new "老顽童工具条" cmd = UI::Command.new("create_sphere"){ LWT.create_sphere # 调用创建球体插件 } cmd.large_icon = cmd.small_icon = "lwt_plug/image/toolSphere.png" cmd.tooltip = "创建球体" cmd.status_bar_text = "这是创建球体的插件" toolbar.add_item cmd toolbar.show menu.add_item cmd

    我们现在将这个铅笔工具添加到这个工具栏中

    1、去lwt_plug文件夹中新建一个pen_tool.rb文件。

    2、然后修改lwt_load.rb文件

    # 在files数组中追加一条铅笔工具目录 files = [ File.join(path, 'lwt_plug/sphere.rb'), File.join(path, 'lwt_plug/pen_tool.rb'), ] # 在工具条新增一个铅笔工具命令 pen_cmd = UI::Command.new("pen_tool"){ UI.messagebox('此处调用铅笔工具') # 此处调用铅笔工具插件 } pen_cmd.large_icon = pen_cmd.small_icon = "lwt_plug/image/toolPen.png" pen_cmd.tooltip = "铅笔工具" pen_cmd.status_bar_text = "这是铅笔绘制工具" toolbar.add_item pen_cmd

    我们保存代码,看下效果: 能看到这个效果,标识我们已经将铅笔工具加载到了工具条。

    重点来了

    主要功能将会在pen_tool.rb文件中实现。在这个功能中我们将会用到onMouseMove及onLButtonDown方法。还会用到Tool模块下的方法有:

    activate - 当工具被选择后,SketchUp就会调用该方法,一般放置实例变量在这个方法中。例如:鼠标状态等

    deactivate - 当我们释放该工具时会调用该方法

    resume - 当工具在暂停后再次激活时,SketchUp会调用该方法

    onCancel - 当我们在做一些撤销操作时,该方法会在被执行撤销之前被调用

    onSetCursor - 当工具需要设置光标时,SketchUp会调用该方法

    getExtents - 为了准确的绘制图形时,SketchUp需要通过该方法知道其绘制范围

    draw - 该方法是在绘制前用于预览的效果

    来看一下code

    activate

    在这个方法中我们设置两个鼠标点变量,并设置状态栏提示信息

    def activate @mouse_ip = Sketchup::InputPoint.new @picked_first_ip = Sketchup::InputPoint.new if @picked_first_ip.valid? Sketchup.status_text = '选择结束点' else Sketchup.status_text = '选择开始点' end end

    deactivate

    当我们释放工具时,刷新SketchUp视图

    def deactivate(view) view.invalidate # 刷新Sketchup视图 end

    resume

    当工具再次激活是,刷新状态栏提示信息。

    def resume(view) if @picked_first_ip.valid? Sketchup.status_text = '选择结束点' else Sketchup.status_text = '选择开始点' end view.invalidate end

    onCancel

    清除当前变量值,刷新视图界面

    def onCancel(view) @picked_first_ip.clear if @picked_first_ip.valid? Sketchup.status_text = '选择结束点.' else Sketchup.status_text = '选择开始点' end view.invalidate end

    onSetCursor

    设置光标

    def onSetCursor UI.set_cursor(632) # 这里632是软件自带的铅笔光标 end

    onMouseMove

    当鼠标在移动过程中获取鼠标位置信息

    def onMouseMove(flags, x, y, view) if @picked_first_ip.valid? @mouse_ip.pick(view, x, y, @picked_first_ip) else @mouse_ip.pick(view, x, y) end view.tooltip = @mouse_ip.tooltip if @mouse_ip.valid? view.invalidate end

    onLButtonDown

    判断第一个点是否有效,如果有效,我们就添加一条线,检索这条边构成面的个数

    如果面的个数大于0,说明线与线有交叉,清除第一个鼠标点,

    否则将移动的点复制给@picked_first_ip。

    def onLButtonDown(flags, x, y, view) num_new_faces = 0 if @picked_first_ip.valid? model = Sketchup.active_model model.start_operation('Edge', true) edge = model.active_entities.add_line(@points) num_new_faces = edge.find_faces model.commit_operation num_new_faces end if num_new_faces > 0 @picked_first_ip.clear Sketchup.status_text = '选择开始点' else @picked_first_ip.copy!(@mouse_ip) end if @picked_first_ip.valid? Sketchup.status_text = '选择结束点.' else Sketchup.status_text = '选择开始点' end view.invalidate end

    getExtents

    将所有的有效点存在points数组中,然后将这些点放入新建的boundingbox对象中。以便我们在视图中更好的观察到。

    def getExtents bb = Geom::BoundingBox.new @points = [] @points << @picked_first_ip.position if @picked_first_ip.valid? @points << @mouse_ip.position if @mouse_ip.valid? bb.add(@points) bb end

    draw

    在绘制线段之前,我们先把效果预览出来。即符合所见即所得的思想。

    def draw(view) return unless @pints return unless @points.size == 2 view.set_color_from_line(*@points) view.line_width = 1 view.line_stipple = '' view.draw(GL_LINES, @points) @mouse_ip.draw(view) if @mouse_ip.display? end

    结束 喜欢关注公众号【老顽童与小东邪】获取更多教程

    下期演示效果

    Processed: 0.009, SQL: 9