Solving Drill.rb Problem

Dear Friends,

This is an example that written by our friend Jason E. Holt. This plugins can make a hole in a box but if box is a group or component, this plugin has malfunctioning. Can any one solve this problem?

Hacked together from the linetool example by Jason E. Holt

Copyright 2005-2008, Google, Inc.

This software is provided as an example of using the Ruby interface

to SketchUp.

Permission to use, copy, modify, and distribute this software for

any purpose and without fee is hereby granted, provided that the above

copyright notice appear in all copies.

THIS SOFTWARE IS PROVIDED “AS IS” AND WITHOUT ANY EXPRESS OR

IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED

WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

#-----------------------------------------------------------------------------

require ‘sketchup.rb’

class Drill

@@drill_diameter = 1

This is the standard Ruby initialize method that is called when you create

a new object.

def initialize
end

def draw(view)
Sketchup::set_status_text “DIAMETER”, SB_VCB_LABEL
Sketchup::set_status_text @@drill_diameter.to_s, SB_VCB_VALUE
end

The activate method is called by SketchUp when the tool is first selected.

it is a good place to put most of your initialization

def activate
Sketchup::set_status_text “DIAMETER”, SB_VCB_LABEL
Sketchup::set_status_text @@drill_diameter.to_s, SB_VCB_VALUE
end

The onLButtonDOwn method is called when the user presses the left mouse button.

def onLButtonDown(flags, x, y, view)
center_ip = Sketchup::InputPoint.new
center_ip.pick view, x, y
face = center_ip.face

center = center_ip.position
normal = face.normal

if ( face )
  circle = view.model.entities.add_circle center, normal, @@drill_diameter / 2.0

  other_entities = face.all_connected

  reversed_normal = normal.reverse

  circleface = nil

  # This seems lame.  add_circle seems to add it as a face, but then I have
  # to go looking for that face.
  for other in other_entities
if other.typename == "Face" and other.classify_point(center) == 1
  #UI.messagebox("found myself :/")
  circleface = other
end
  end

  # Find a face opposite the one they clicked in
  for other in other_entities

    #UI.messagebox("type is " + other.typename)

    if other.typename == "Face"
      if reversed_normal.samedirection? other.normal
        #UI.messagebox "found it!"
    
    point_on_other_face = center.project_to_plane other.plane
    if other.classify_point(point_on_other_face) == 1
      #UI.messagebox "yes, point projects onto other face."
      pushpull_distance = point_on_other_face.vector_to(center).length

      circleface.pushpull(-pushpull_distance)
    else
      #UI.messagebox "nope, doesn't project"
    end
      else
        #UI.messagebox "nope"
      end
    end
  end

  view.model.commit_operation

else
  UI.messagebox "Drilling only works on faces"
end

end

def onUserText(text, view)

# The user may type in something that we can't parse as a length
# so we set up some exception handling to trap that
begin
    value = text.to_l
rescue
    # Error parsing the text
    UI.beep
    puts "Cannot convert #{text} to a Diameter"
    value = nil
    Sketchup::set_status_text "", SB_VCB_VALUE
end
return if !value

@@drill_diameter = value

end

end # class Drill

#-----------------------------------------------------------------------------

This functions is just a shortcut for selecting the new tool

def drill
Sketchup.active_model.select_tool Drill.new
end

UI.menu(“PlugIns”).add_item(“Drill”) {
drill
}

Would you please so kind to edit your post following this guide:

…and put it - in my opinion - better category to: Developers: Ruby API

BTW.
If you change the code in line 49
circle = view.model.entities.add_circle center, normal, @@drill_diameter / 2.0
to
circle = view.model.active_entities.add_circle center, normal, @@drill_diameter / 2.0

and you go into the group or component editing context (open the group or component for editing ) before you start the tool it will make the hole …

1 Like

Wow you are so cleaver and your answers always help me a lot. if I am in group edit mode, your code works but I don’t know in ruby how I can pick a group and start editing it. Can you help me for it?

Sure. But only if you correct the first post as I asked:stuck_out_tongue_winking_eye:

More seriously:
Note: This will work only if SketchUp version is higher than 2020.0.

You can use Modell#active_path= instance_method

For the parameter you can give a
InstancePath but you have to check it’s validity before by
InstancePath#valid?instance_method

Fortunately there is a
InputPoint#instance_path-instance_method
to use in your case.

In summary, concluding with e.g. something like this :
You have to define an onMouseMove method inside your Drill class method:

def onMouseMove(flags, x, y, view)
  center_ip = Sketchup::InputPoint.new
  center_ip.pick view, x, y
  view.model.active_path = center_ip.instance_path.valid? ? center_ip.instance_path : nil
end

When you will move your mouse the editing context will “automatically” change to that where a pointer is located. Therefore when you click the hole will be inside the right instance…

it seems I asked wrong question. I changed drill program as you said. if I go into the group editing context and select a face it will make a hole on it but if I don’t go into the group editing context program delete group. I wish to solve this problem. I mean when I click on a group, automatically I go into its editing context and make hole. Thank you for your attention.

If you are inserting my code mentioned my previous post into the Drill.rb, you do not need to go manually into the group editing context anymore. (You have to keep my first advice as well:
'circle = view.model.active_entities.add_circle center, normal, @@drill_diameter / 2.0'
Just try it.
There are several other problem inside the Drill.rb, which I not willing to rewrite for you.

image

after inserting
image

1 Like

I really don’t know how thanks you!!! it works well. wall.rb (5.9 KB)
it is a program that I made by your help. Please let me know if you have some advice for it. Thanks again.

I don’t really see a close connection between your wall.rb and the code I suggested in this topic …
Never mind. If you are happy with it I’m happy too. You can use it your own, but I suggest you: do not publish!

I opened your wall.rb and find some improvable details but after about 30 sec I had to immediately closed it…
…For the simple reason that you ignored my suggestions. Namely, as I mentioned at least 3 times:

AVOID USING GLOBAL VARIABLE ( starting with $ )

_
Secondly: I kindly asked you -two times - to correct your first post. You didn’t even noticed it.

Have a nice day!

Dear Friend, Sorry for misunderstanding. Software I sent to you is different from my question here. In fact, first software is Wall that I finish it. Second software is Window and need make hole in wall for it.Drill.rb (2.1 KB) . As you can see in this software I used your advice and it works really well although it really confused me and I cannot find any source for it.

Unfortunately it seems that you are ignoring any possible advice not only mine. But yet more important rules like copyright.
The file what is in your post above DOES NOT contain the important part about the copyright notice what must be included.
Why don’t / can’t you read and understand at least YOUR first post?
I highly recommend that you think about it and withdraw the attachment of your last post.

I dare not even mention my other requests anymore, because it’s like water off a duck’s back.
Good luck!

If you talk about Wall.rb, it is my own code. if you talk about Drill.rb, at first I mentioned code source and he also let us to use his code. I don’t know which copyright I ignored. I am so sorry if I ignored copyright and If I did not follow the copyright law, it was unintentional.

Do what you want.
I give up.
I’m out.

Sorry again and thank you so much for all of your help. I wish you best… I will mentioned your name in codes you send to me whenever I deliver my plugins.

No way! Please DO NOT mention me!
What I’ve helped so far I’ve been happy to do, but that’s it!
Have a nice and healthy time!