Push Pull direction?
-
You are making this look very complicated.
Can you explain in simple words [not code] what you want to achieve?
Where have you definedMINDAP=10.mm
etc and all of your other constants ?
Do this in the body of the class before any methods are called, or even inside thedef initialize()
...
Where have you defined the prompts[DiaP: "", etc]
where have you defined the default values[@diaP, etc]
Also don't use$diaP
etc
use@diaP
etc instead, AND give them a default value, e.g.
@diaP=MINDAP if not @diaP
inside thedef initialize()
of the class so if it's the first run it usesMINDAP
or if later the current value of@diaP
This is the simple form...
results = inputbox(prompts, values, "Specifikationer") return if not results diaP, diaA, krvD, krvB = results
THEN test for compliance ofdiaP
etc and redo the dialog...
Finally exiting with@diaP, @diaA, @krvD, @krvB = results
-
OK - some restructering but here is a code that WILL run correctly as it is now (WITH the out-commented lines 49, 56 and 68 !)
IF you "activates" the WHILE loop again (remove =begin, =end and the end to the WHILE in the lines 49, 56 and 68)
it reports an error missing an "end" in the end ??????????????????????=)(?/??=)(#?=)(?=#(€/(#)=#&€&
The meaning of the WHILE loop is to secure that some MINIMUM input values are obtained.HOW CAN ACTIVATING THE WHILE LOOP CAUSE THIS ERROR missing an "end" in the end when it was no problem before - what is wrong here ?
(For now never mind the modul and class lines)
Here is the code:
#module KSOR # class polley # store bogstaver evt. med _ er en CONST MINDIAP = 40.mm MINDIAA = 10.mm MINKRVD = 20.mm MINKRVB = 15.mm # First check for PC or MAC platform = (Object;;RUBY_PLATFORM =~ /mswin/i) ? ;windows ; ((Object;;RUBY_PLATFORM =~ /darwin/i) ? ;mac ; ;other) if platform.to_s=="windows" then # pull in the standard API hooks. require 'sketchup.rb' # Get handles to our model and the Entities collection it contains. model = Sketchup.active_model $entities = model.entities # Show the Ruby Console at startup so we can # see any programming errors we may make. Sketchup.send_action "showRubyPanel;" # Add a menu item to launch our plugin. UI.menu("PlugIns").add_item("Kileremskive") { # Call our new method. draw_pulley } else UI.messagebox("Denne facilitet er KUN til Windows !", MB_OK) end # end main program ----------------------------------------------------------------------------- def draw_pulley # labels for input fields prompts = [$exStrings.GetString("Skivediameter (mm) ;"), $exStrings.GetString("Akseldiameter (mm) ;"), $exStrings.GetString("Kravediameter (mm) ;"), $exStrings.GetString("Kravebredde (mm) ;")] # default MINIMUM values $diaP = MINDIAP $diaA = MINDIAA $krvD = MINKRVD $krvB = MINKRVB =begin while ($diaP < MINDIAP) || ($diaA < MINDIAA) || ($krvD < MINKRVD) || ($krvB < MINKRVB) # test for minimum values in ALL - change the one less than minimum if ($diaP < MINDIAP) then $diaP = MINDIAP if ($diaA < MINDIAA) then $diaA = MINDIAA if ($krvD < MINKRVD) then $krvD = MINKRVD if ($krvB < MINKRVB) then $krvB = MINKRVB =end values = [$diaP, $diaA, $krvD, $krvB] # Now display the input dialog results = inputbox prompts, values, $exStrings.GetString("Specifikationer") # Returner if no inputs return if not results $diaP, $diaA, $krvD, $krvB = results if ($diaP < MINDIAP) || ($diaA < MINDIAA) || ($krvD < MINKRVD) || ($krvB<MINKRVB) then UI.messagebox("MindstevÊrdi er genindsat - de(n) SKAL overholdes !", MB_OK) end # end draw_beltProfile set_beltProfileOnWheel pull_pulley pull_krave end def draw_beltProfile pt1 = [0,4.22.mm,0] pt2 = [13.8.mm,0,0] pt3 = [13.8.mm,12.7.mm,0] pt4 = [0,8.48.mm,0] # Call methods on the Entities collection to draw stuff. $fullProfile= $entities.add_face pt1, pt2, pt3, pt4 $beltProfile = $entities.add_group $fullProfile.all_connected end def set_beltProfileOnWheel # Her laves en profil, som svarer til en kilerem # # Skive laves $periferi = $entities.add_circle [0,0,0], [0,0,1],$diaP.to_f/2,48 # transformation af helpLine pÄ $beltProfile til Origo tr = Geom;;Transformation.translation [-11.55.mm,-0.69.mm,0] $beltProfile.transform! tr # og videre til cirkelperiferien tr = Geom;;Transformation.translation [$diaP.to_f/2,0,0] $beltProfile.transform! tr # rotate beltProfile 90° tr = Geom;;Transformation.rotation [13.8.mm,0,0], [1,0,0], 90.degrees $entities.transform_entities tr, $beltProfile # gruppen exploderes for at lave FollowMe og findes sÄ ikke mere $beltProfile.explode $fullProfile.followme $periferi end def pull_pulley # Nu skal siderne pÄ skiven laves 2 mm pÄ hver side opSidePeriferi = $entities.add_circle [0,0,12.mm], [0,0,1], $diaP.to_f/2 + 2.25.mm, 48 opSideFlade = $entities.add_face opSidePeriferi opSideFlade.pushpull 2.mm nedSidePeriferi = $entities.add_circle [0,0,-0.7.mm], [0,0,-1], $diaP.to_f/2 + 2.25.mm, 48 nedSideFlade = $entities.add_face nedSidePeriferi nedSideFlade.pushpull 2.mm cleanUp end def cleanUp # En cylinderflade fjernes, sÄ der Äbnes ind til remprofilen end def pull_krave # En krave til nippel skruer laves efter angivne dimensioner # og der skal laves hul til akslen med den givne diameter end # end #class #end # module
-
I have put your post's 'code' inside 'code' []braces[/] - it's impossible to read meaningfully otherwise.
Please learn to use these...A few general comments...
I know the class is not currently activated but it should be**P**olley
The menu item should be something like
UI.menu("PlugIns").add_item("Kileremskive"){KSOR::Polley.draw_pulley()}
Putrequire 'sketchup.rb'
before the module starts - it only gets used if sketchup.rb hasn't been loaded before - it is a failsafe, if you are using any methods that this system script might need to have loaded...
Why do you need to check whether this is a PC or MAC tool ?
Most scripts/tools run just fine on both platforms you only need to check when some code is platform specific...
Please learn to use @ NOT $ variables.
Also
if ($diaP < MINDIAP) then $diaP = MINDIAP
will work without and 'end' IF it's this way round
$diaP = MINDIAP if $diaP < MINDIAP
[although you do need to test if the variable is set with
$diaP = MINDIAP if not @diaP or @diaP < MINDIAP
also use@diaP
NOT$diaP
!]
You don't normally use/need a 'then' in an 'if' test in Ruby, although it won't beak anything, BUT you do need the 'end' unless the result is place before the 'if' as I have shown...
Also learn to enclose arguments inside () with no space between the opening ( and its method - 'loose' arguments may be deprecated in future Ruby updates and therefore the API docs are all pretty much out of date...
You could probably do with less transformations/exploding etc if you placed objects where you wanted them initially. I'd recommend you put all of your new geometry inside a group [or component] as it means there are no unexpected clashes with existing geometry... I haven't changed much of this though...I've recast your code into a working example for you [attached] - see how I have adjusted parts and then feel free to ask us more questions...
-
OK - nice with your comments and alternative if's and I'll study that closely BUT
What about my WHILE-problem ?
Do you mean that IF I DO like you said then this problem will disappear or .. ? -
Read my edited post and the attachment...
The end was because the 'if' aren't properly formed and the first one 'steals' the while's end.
BUT my example shows how to avoid this 'while' loop altogether !
There were a number of overlaid issues - it's best to see my alternative reworking on how some things are just avoided/omitted or alternative forms will work as you hope... -
OH, sorry I didn't see there was an attachment - sorry !
-
@tig said:
Read my edited post and the attachment...
The end was because the 'if' aren't properly formed and the first one 'steals' the while's end.
BUT my example shows how to avoid this 'while' loop altogether !
There were a number of overlaid issues - it's best to see my alternative reworking on how some things are just avoided/omitted or alternative forms will work as you hope...Yeah, but now you took it a little further than I can cope with !
AND
you should have tried to input a lower value than the minimum allowed - if you do you get this error:
Error: #<NoMethodError: private method
initialize' called for #<KSOR::Polley:0xa9cd790>> C:/Program Files/Google/Google SketchUp 8/Plugins/RemSkive.rb:43:in
dialog'
C:/Program Files/Google/Google SketchUp 8/Plugins/RemSkive.rb:25:ininitialize' C:/Program Files/Google/Google SketchUp 8/Plugins/RemSkive.rb:5:in
new'
C:/Program Files/Google/Google SketchUp 8/Plugins/RemSkive.rb:5and NO re-asking of new input but that's surely caused by the error.
-
OK simple typo - simple fix.
THIS version recalls the dialog on an invalid entry...
See how I split a 'draw_pulley' method off from the un-callable 'initialize' that I had inadvertently typed....
You should recognize much of your code/steps in the reworked version - I've just avoided the pitfalls of your while-loop, un-ended if's etc etc...
-
$exStrings.GetString("Specifikationer")
will NOT work. All LanguageHandler keys are English. The values that are returned, are in the other language.You can open the strings files Notepad++ to see what the English key are.
Or at the console:
$exStrings.GetStrings.each_pair{|k,v| puts( %[key: "#{k}" = "#{v}"] )}
You can only access
$exStrings
if the Examples extension is enabled by the user. You should not rely upon it. -
@dan rathbun said:
$exStrings.GetString("Specifikationer")
will NOT work. All LanguageHandler keys are English. The values that are returned, are in the other language.You can open the strings files Notepad++ to see what the English key are.
Or at the console:
$exStrings.GetStrings.each_pair{|k,v| puts( %[key: "#{k}" = "#{v}"] )}
You can only access
$exStrings
if the Examples extension is enabled by the user. You should not rely upon it.
I've removed these from my test code as he doesn't need them, neither does he need to test for 'platform' either...
I think that he's trying to apply methods and ideas from several sources and thereby making a script that's much more convoluted that it needs to be...
At the moment it's actually a simple script to ask for some dimensions and create a 3d pulley wheel from them... -
I'll study the code and compare it with my thinking of Class, Objects, properties ect.
But I beleave this is "the right! structure or ... ?
The next Object - let's a a drivebelt - should use this code as "template" (the structure) or ...?
-
I think you are being over analytical.
There are many things you can do with Ruby, but in your case checking 'platform', 'translating' strings and so on... are far more than you need to do yo complete what you are trying for.
Meanwhile there are a few things that you are not doing, which we'll skim over for now as 'sufficient is fit'... it works without them and they are 'nice-to-haves'... -
Ha, ha, too analytical - you can't be when working with OOP, I beleave.
I'm just trying to get a nice structure from begining of my Ruby eara.
Dan
???? I don't know what you mean:
"$exStrings.GetString("Specifikationer") will NOT work. All LanguageHandler keys are English. The values that are returned, are in the other language."It DID work fine, eventhough I can see TIG changed it - in fact it did work.
My test for Mac or PC was code I found and I thought that code for Mac WAS special so why not make a test - but OK it's obsolete.
Now I'll try to finish the pulley thing !
-
The 'getStings' coded parts won't 'work' because it won't find your Danish word to translate in the standard '.stings' file, which is in English !
Adding that code does no real harm, other than slowing things down slightly, because every time it checks for a translation it doesn't find it and uses the original string anyway...
The principal of a 'language translator' tool is that the strings in the script-file also exist in a 'look-up' file - typically a '.strings' file [or for purpose made translators like my deBabelizer.rb it a .lingvo files]; the English word is matched against the foreign word FR Cat<==>Chat or ES Cat<==>Gato etc. I don't think Danish is a Sketchup 'locale' though. However if you are using an EN-US locale with a script using my deBabelizer lingvos appropriately then taking the standard ...EN-US.lingvo and swapping the Cat<==>Cat for Cat<==>Kat etc etc would display menus,prompts,dialogs etc in Danish strings rather than English...
I suspect this is something not high on your agenda...The issue of testing for PC/MAC only arises when you do something that is platform specific - most simple API code is cross-platform; however, you might one day move on to Tools which for example involve key-presses, and these key-codes can vary between PC and MAC so you need to use different code depending on the platform test results; there are also add-ons like Win32ole.so and Win32api.so that add links from Ruby into the Windows system and even other app's like Excel - these cannot be used on a MAC so again you need to trap for that possibility... BUT your relatively simple code needs no sure 'traps'...
-
@ksor said:
Dan
???? I don't know what you mean:
"$exStrings.GetString("Specifikationer") will NOT work. All LanguageHandler keys are English. The values that are returned, are in the other language."It DID work fine, eventhough I can see TIG changed it - in fact it did work.
What TIG said is true.
You don't need to worry about translating plugins just yet.
$exStrings.GetString("khbgjubgeg663buib")
returns >> "khbgjubgeg663buib"
... because the key "khbgjubgeg663buib" does not exist in the hash, and theLanguageHandler
just echos the key (assuming it would be a valid English string.)It is a fallback feature. (I suppose Google should have output a "Key not found" warning to the console.)
-
OK - I see it's for internationalization of the code - I misunderstood this in the first place.
-
@tig said:
OK simple typo - simple fix.
THIS version recalls the dialog on an invalid entry...
See how I split a 'draw_pulley' method off from the un-callable 'initialize' that I had inadvertently typed....
You should recognize much of your code/steps in the reworked version - I've just avoided the pitfalls of your while-loop, un-ended if's etc etc...Now I'm a little over analytical again - I think your code here contains a bomb:
def draw_pulley() # default MINIMUM values @diaP = MINDIAP if not @diaP or (@diaP < MINDIAP) @diaA = MINDIAA if not @diaA or (@diaA < MINDIAA) @krvD = MINKRVD if not @krvD or (@krvD < MINKRVD) @krvB = MINKRVB if not @krvB or (@krvB < MINKRVB) return nil unless self.dialog() self.draw_beltProfile() self.set_beltProfileOnWheel() self.pull_pulley() self.pull_krave() end def dialog() prompts = ["Skivediameter (mm) ;", "Akseldiameter (mm) ;", "Kravediameter (mm) ;", "Kravebredde (mm) ;"] values = [@diaP, @diaA, @krvD, @krvB] results = inputbox(prompts, values, "Specifikationer") return nil if not results diaP, diaA, krvD, krvB = results if (diaP < MINDIAP) || (diaA < MINDIAA) || (krvD < MINKRVD) || (krvB<MINKRVB) UI.messagebox("MindstevĂŠrdi er genindsat - de(n) SKAL overholdes !", MB_OK) self.draw_pulley() return nil else @diaP, @diaA, @krvD, @krvB = results return true end end
The call to self.draw_pulley is a sort of recursive - just returning nil would be better and then this in the initializing method:
until self.draw_pulley() != nil end
Right ?
-
Sorry, I don't quite understand your point...
The tool initializes, it runs
self.draw_pulley()
, which runsself.dialog()
and tests for the validity of its 'return
'.
If the user cancels the dialog 'nil
' is returned and it stops.
If the user enters valid inputs it returns 'true
' and the remaining steps are executed to completion.
If the user enters invalid input it then re-runsself.draw_pulley()
[], which in turn runsself.dialog()
and tests for its validity in its 'return
' etc etc in a loop until either the user cancels or enters valid input...
[ after it re-calls the method it immediately returns 'nil
' to the previously executing version of the method, so that one goes no further]
Note how two sets of references are taken from the 'results
', one to test their validity and if OK then another@
set to be used in the next methods and also remembered for the next time...
I left it this way because that's what you originally showed... personally I feel that a simple
UI.messagebox("Values outside limits !\nExiting.") return nil
would do, but at least this way the user does get chances to try again with a valid input in a new dialog - if they want to give up they can just cancel the dialog at any time...
Advertisement