SketchUcation Premium Membership

 

 

Objets controlled by a webpage

Objets controlled by a webpage

Postby Ericschimel » Sun Jun 07, 2009 2:15 pm

Hi all..

I am trying to write a plugin that can control Sketchup objects from a web dialog that is on a website, not a local HTML file.

I see from this video, http://www.youtube.com/watch?v=FALvwBN5 ... re=related (If you fast forward to 6:08) that you can grab data off of a website and render it in Sketchup....

Does anyone out there have a sample of this code? I'd like to see it in action... I have searched for that plugin that Scott demonstrated on there, but I can't seem to find it...
-Eric
http://www.SketchThis.Net
Sketchup Training & Videos
Custom Models
Ericschimel
 
Posts: 227
Joined: Mon Jun 16, 2008 5:37 pm

Re: Objets controlled by a webpage

Postby Jim » Sun Jun 07, 2009 2:31 pm

Hi Eric,

I just wanted to point out the engineering toolbox page does something similar.

They have a minimal ruby plugin that is installed, which opens a WebDialog poited to their server.

Then, they send some Ruby code to the dialog, and 'eval' it; but that is only one way to do it. You could have all of your callbacks defined in the plugin, for example.

View the source code of the plugin, and the source of one of the HTML pages to see how it works.


http://sketchup.engineeringtoolbox.com/
Jim
Global Moderator
 
Posts: 4126
Joined: Mon Nov 12, 2007 10:13 pm
Location: NEOH
Name: Jim
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: other
Level of SketchUp: Intermediate

Re: Objets controlled by a webpage

Postby Ericschimel » Wed Jun 10, 2009 9:22 pm

Awesome! I will check it out!
-Eric
http://www.SketchThis.Net
Sketchup Training & Videos
Custom Models
Ericschimel
 
Posts: 227
Joined: Mon Jun 16, 2008 5:37 pm

Re: Objets controlled by a webpage

Postby Ericschimel » Thu Jun 11, 2009 2:15 pm

So I checked that out, its a pretty neat web page.

In some of the models, you can configure options of the model right on the web page, and then insert it into Sketchup, pre-configured.

There is a Javaspript that runs when you configure the model.

I am confused when the actual configuring happens though.... Does the user configure the model, then the model gets sent to Sketchup, where Sketchup then receives the configuration choices from the script, the changes are made, and then the model is dropped in? (That is a long sentence, and my only guess!)

Anyone else have any insights? I am just learning Ruby, and I have just about zero Java experience.
-Eric
http://www.SketchThis.Net
Sketchup Training & Videos
Custom Models
Ericschimel
 
Posts: 227
Joined: Mon Jun 16, 2008 5:37 pm

Re: Objets controlled by a webpage

Postby Jim » Thu Jun 11, 2009 4:48 pm

If you edit their plugin and add the line just before the eval statement

Code: Select all
puts(p.inspect)
eval(p)


Open the Ruby Console to see the Ruby code that the site is sending, and their plugin is eval'ing.
Jim
Global Moderator
 
Posts: 4126
Joined: Mon Nov 12, 2007 10:13 pm
Location: NEOH
Name: Jim
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: other
Level of SketchUp: Intermediate

Re: Objets controlled by a webpage

Postby Ericschimel » Sun Jun 14, 2009 12:40 am

Code: Select all
"compName = \"Cone\";componentdefinition = nil;o_red = 0;o_green = 0;o_blue = 0;comp_col = \"\";o_point = Geom::Point3d.new o_red,o_green,o_blue;o_transform = Geom::Transformation.new o_point;  dt = 0;  db = 39.37;  h = 39.37;  centerpoint = Geom::Point3d.new;  vector = Geom::Vector3d.new 0,0,1;  vector2 = vector.normalize!;  model = Sketchup.active_model;  entities = model.active_entities;  group = entities.add_group;  group.name = compName;  entities = group.entities;  edgearrayO = entities.add_circle centerpoint, vector2, db/2;  edgeO = edgearrayO[0];  arccurveO = edgeO.curve;  faceO = entities.add_face edgearrayO;  pts = [];  pts[0] = [0, 0, h];  pts[1] = [0, 0, 0];  pts[2] = [0, db/2, 0];  if dt != 0 then pts[3] = [0, dt/2, h] end;  base = entities.add_face pts;  unless comp_col == \"\";    base.material = Sketchup::Color.new comp_col;    base.material.alpha = 0.99;  end;  base.followme edgearrayO;group.move! o_transform;if model.selection[0];s = model.selection[0]; t = s.transformation; group.transform! t;end;"
onSelectionBulkChange1
Webdialog set to#<Sketchup::Group:0xb908078>
onSelectionBulkChange0


So that's what I get in the Ruby console.

I am assuming that these are all Ruby commands that are initiated by a Javascript that gathers all the user input from the website.

I checked out the Javascript, not knowing anything about Java, its really hard to tell what's going on.
Where would I go to learn how to get Java to talk to ruby? I think if I see it in its most basic example, I could build from there...
-Eric
http://www.SketchThis.Net
Sketchup Training & Videos
Custom Models
Ericschimel
 
Posts: 227
Joined: Mon Jun 16, 2008 5:37 pm

Re: Objets controlled by a webpage

Postby Todd Burch » Sun Jun 14, 2009 2:35 am

Javascript is not Java - completely different language.

Todd
Todd Burch
 
Posts: 909
Joined: Mon Nov 12, 2007 2:43 pm
Location: Katy, Texas

Re: Objets controlled by a webpage

Postby chrisglasier » Sun Jun 14, 2009 3:08 am

You might find something helpful about javascript/ruby interaction from the code accessible from here

Chris

PS I'm interested in doing this remotely as well
User avatar
chrisglasier
 
Posts: 1014
Joined: Tue Jun 03, 2008 9:11 am
Location: Hong Kong/Zhuhai
Name: Chris Glasier
Operating system: Windows
SketchUp version: 8
License type: Free/Make
SketchUp use: other
Level of SketchUp: Intermediate

Re: Objets controlled by a webpage

Postby Ericschimel » Sun Jun 14, 2009 5:19 pm

Ok, so here's what I have up and running..........

http://sketchupapi.blogspot.com/2008/02 ... y-and.html

What I am a little fuzzy on here is how that "refresh" button actually sends the command to Ruby. I see part of the command in the HTML file, but I am not sure exactly how it works....

I would assume that somehow the command is created by variables being pieced together, eventually creating a command, based on user input.

I think if I could just understand how the command is created in the HTML file, I could then create buttons that could do anything that I want in the web dialog....
-Eric
http://www.SketchThis.Net
Sketchup Training & Videos
Custom Models
Ericschimel
 
Posts: 227
Joined: Mon Jun 16, 2008 5:37 pm

Re: Objets controlled by a webpage

Postby chrisglasier » Mon Jun 15, 2009 12:29 am

Ericschimel wrote:Ok, so here's what I have up and running..........

http://sketchupapi.blogspot.com/2008/02 ... y-and.html

What I am a little fuzzy on ...


What I am fuzzy on is what you have done and really what you want to do. This is mainly because youTube and blogSpot are not available in China right now. But anyway in the forums posters normally post their code or whatever directly in the post using links for backup; this makes it easier to respond to requests.

... how that "refresh" button actually sends the command to Ruby. I see part of the command in the HTML file, but I am not sure exactly how it works....


I don't know if you are referring to my refresh function; it refreshes the web dialog content after the cursor returns from the SU display. The parts that are relevant are:

    Javascript functions that send the "skp:something@"+params to call Ruby callbacks
    Ruby callbacks that execute the something in the SU display or return something for further javascripting

However, this interaction is not the only thing concerned with both local and remote control. Identification of entities and their properties is high on the list. I use javascript arrays to mirror their Sketchup counterparts. Simply, if you want to move an object you need to identify it, find its current location and determine the new XYZ increments in 3D space. Here is a callback that actually does the moving (mostly made from Jim's snippets):

Code: Select all

@dlg.add_action_callback("mover") {|d, p|
   
a = p.split(",")
   
entityNo =  Integer(a[0])
theX = Integer(a[1]).mm
theY = Integer(a[2]).mm
theZ = Integer(a[3]).mm
         
model = Sketchup.active_model
entities = model.active_entities
entity = entities[entityNo]
      
point = Geom::Point3d.new theX, theY, theZ
t = Geom::Transformation.new point
   
entity.transform! t    }




Then of course there's rotations.

angle_between 90 90 90.jpg


It all can get a little fraught but then what we are both (I think) trying to achieve is novel (i.e. not openly technically supported, and not only about finding technical solutions).

... I could then create buttons that could do anything that I want in the web dialog....


I believe this to be true and a solid base for Sketchup to become as normal part of daily work as word processing has been.

Write some more if I have read you right.

Chris.
Please, register (free) to access all the attachments on the forums.
User avatar
chrisglasier
 
Posts: 1014
Joined: Tue Jun 03, 2008 9:11 am
Location: Hong Kong/Zhuhai
Name: Chris Glasier
Operating system: Windows
SketchUp version: 8
License type: Free/Make
SketchUp use: other
Level of SketchUp: Intermediate

Re: Objets controlled by a webpage

Postby Ericschimel » Mon Jun 15, 2009 2:32 am

I think you are on the right track here....

Let me outline what exactly I am trying to do, and what I have learned so far...

What I want is a simple Ruby script that loads a web page right in Sketchup. I've got that worked out already....

I want models to be available on the web page.

I want the user to be able to set options on the models.

After the user has set options, they can insert the model into their model.

I also want the web page to be able to gather information about the model (like part counts, materials, stuff like that)

So far I have learned that this can be achieved through Javascript passing commands to Ruby. Exactly how that works, I am still unclear.

What I would like to do is build this in its most basic form. Say a webpage with one model, with one option that can be configured, and work from there. I just don't have the technical knowledge to get even that basic example started. Once I get that, I should really be able to get a handle on things and start to build it out...
-Eric
http://www.SketchThis.Net
Sketchup Training & Videos
Custom Models
Ericschimel
 
Posts: 227
Joined: Mon Jun 16, 2008 5:37 pm

Re: Objets controlled by a webpage

Postby chrisglasier » Mon Jun 15, 2009 2:54 am

Ericschimel wrote:I think you are on the right track here....


OK let me come back to the detail after you have watched this short video. You could let me know if it works (I cannot see it from here) but more importantly whether the part about selecting SU versions of models from remote sites and manipulating them is closer to your concept. Tell me which parts differ. I think this is less confusing.

Cheers
User avatar
chrisglasier
 
Posts: 1014
Joined: Tue Jun 03, 2008 9:11 am
Location: Hong Kong/Zhuhai
Name: Chris Glasier
Operating system: Windows
SketchUp version: 8
License type: Free/Make
SketchUp use: other
Level of SketchUp: Intermediate

Re: Objets controlled by a webpage

Postby Pout » Mon Jun 15, 2009 10:11 pm

maybe this can be interesting for you:
http://www.modelur.com/home

besides that, i don't know if it is possible to load a php instead of a html file in a webdialog?
If so, you can get the data from a database with php and send it to Sketchup with javascript
Pout
 
Posts: 272
Joined: Thu Aug 21, 2008 10:46 am

Re: Objets controlled by a webpage

Postby Ericschimel » Mon Jun 15, 2009 10:39 pm

This is basically the functionality I am trying to duplicate.....

I just wanted to point out the engineering toolbox page does something similar.

They have a minimal ruby plugin that is installed, which opens a WebDialog poited to their server.

Then, they send some Ruby code to the dialog, and 'eval' it; but that is only one way to do it. You could have all of your callbacks defined in the plugin, for example.

View the source code of the plugin, and the source of one of the HTML pages to see how it works.


http://sketchup.engineeringtoolbox.com/
-Eric
http://www.SketchThis.Net
Sketchup Training & Videos
Custom Models
Ericschimel
 
Posts: 227
Joined: Mon Jun 16, 2008 5:37 pm

Re: Objets controlled by a webpage

Postby Ericschimel » Fri Jun 26, 2009 4:56 am

Scott, are you lurking out there somewhere? ;)
-Eric
http://www.SketchThis.Net
Sketchup Training & Videos
Custom Models
Ericschimel
 
Posts: 227
Joined: Mon Jun 16, 2008 5:37 pm

Re: Objets controlled by a webpage

Postby Jim » Fri Jun 26, 2009 5:49 pm

Hi Eric,

The example you cited from the SketchUp Blog has everything you need.

In the html of the WebDialog there is a button:

Code: Select all
<input type="button" onclick="callRuby('pull_selection_count')" value="Refresh">


onclick is an event that is fired when the button is clicked. When the button is clicked, the Javascript function named callRuby is executed. The string value 'pull_selection_count' is passed to the callRuby function.

Code: Select all
// Javascript in the html which defines the callRuby function used by onclick.
function callRuby(actionName) {
    query = 'skp:get_data@' + actionName;
    window.location.href = query;
}


callRuby shows how to pass a value from the WebDialog to the SketchUp plugin that created the dialog. The Ruby script needs a callback named 'get_data'.

window.location = 'skp:get_data@' + data;
  • skp: - not sure precisely what this does but is required for the callback,
  • get_data - the name of the call back in the ruby script, defined using add_action_callback('callbackname') { ... }
  • @ - Optional: If you need to pass values to the call back, you need this delimiter.
  • + String concatenation in Javascript.
  • data - a Javascript String.
Code: Select all
# The ruby part
my_dialog.add_action_callback("get_data") do |web_dialog, action_name|
  UI.messagebox("Ruby says: Your javascript has asked for " + action_name.to_s)
end


The Ruby variable action_name get the value of the Javascript variable data.

Hope that helps a little.
Jim
Global Moderator
 
Posts: 4126
Joined: Mon Nov 12, 2007 10:13 pm
Location: NEOH
Name: Jim
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: other
Level of SketchUp: Intermediate

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Vertex Tools for SketchUp. Take control over each vertex with this vertex editor for SketchUp.

Premium Members get 20% discount!

Ad Machine
Robot
 
Posts: 2012


Return to Developers' Forum

Who is online

Users browsing this forum: Jenn_Abaunza and 6 guests