Automaticaly try to import files that aren't .sku

Automaticaly try to import files that aren't .sku

Postby Eric_Erb » Thu Oct 08, 2009 9:45 pm

I've been writing a ruby script that automates a bunch of steps I usually have to do by hand. So I've written with a lot of help from you guys 9 little ruby scripts so far that take care of most of the steps. As I get closer to being complete with all of them, I get more worried about finding a solution to the very first step. STEP1... Opening the file. It's easy enough to set the default program for a file type the problem is I'm opening .dxf not sku. I am of course using Sketchup Pro. My question is this... Is there a way with ruby to tell sketchup to try to import the file when it can't directly open it? Or, just add the ability for sketchup to open .dxf?
0

Eric_Erb 
 

Re: Automaticaly try to import files that aren't .sku

Postby Chris Fullmer » Thu Oct 08, 2009 11:29 pm

Use the model.import method:

http://code.google.com/apis/sketchup/do ... tml#import

It looks like you just supply the name+extension of the file, and it will choose the correct file importer for you. See if that works,

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: Automaticaly try to import files that aren't .sku

Postby Eric_Erb » Fri Oct 09, 2009 12:22 am

Chris Fullmer wrote:Use the model.import method


I know this command but what I'm looking to do is write something that will automatically try to import a file if you get the "This Does Not Appear To Be A Sketchup Model!" dialog. Or, really anything that will result in SU opening the dxf when it's double clicked. Should I maybe compile an executable ruby script that says open SU and run the model.import command with the "file name" portion of the script derived from the source file (original NAME.dxf file)? Then I could set that as the default program for dxf files. And by the way, I have no idea how to do what I just suggested.
0

Eric_Erb 
 

Re: Automaticaly try to import files that aren't .sku

Postby TIG » Fri Oct 09, 2009 11:05 am

You aren't 'importing' the dxf file. but rather you want to be 'opening' it in Sketchup ?

Your system will usually have a default application setup to open the dxf - a CAD type app.

If you change the application to Sketchup it will try to open it and fail as it isn't a Sketchup file ! This failure will be before you have a chance for a Ruby to kick in...

You could write an 'applet' that is run when 'opening' the dxf as its default application. That needs to open a new skp and import the dxf... good luck ! :geek:

The easy way is to do it by hand - to open a new empty skp file and import the dxf either from the menu or by a Ruby...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Automaticaly try to import files that aren't .sku

Postby Gruff » Fri Oct 09, 2009 5:35 pm

Can Ruby Scripts Accept a command line parameter on load?

Does Sketchup have command line parameters itself that can load a Ruby Script?

I'd say these were key to cobbling together a solution.
0
Resistance is .... Character Forming. *Grin*

Gruff 
 

Re: Automaticaly try to import files that aren't .sku

Postby Gruff » Fri Oct 09, 2009 7:29 pm

Okay, answering my own question I cannot find any doumentation that says that Sketchup has command line parameters to load a Ruby Script.

Another method would be to have the DXF file launch a Batch File when double clicked. The batch file would take the selected filename as an arument and use it to write a ruby script in the plugIn folder then launch Sketchup.

Following this Idea I wrote a small ruby script that should import a DWG.
It fails though. I can manually select 'File | Import' in Sketchup and the same dxf imports perfectly. Can someone look over my script?

--- LoadDWG.rb ---
model = Sketchup.active_model
show_summary = true
status = model.import "C:\AAA\ACIS Files\WKT 2424 A4O.dwg", show_summary
--- End LoadDWG.rb ---

This is the error I am getting.
--- Error ---
Error opening AutoCAD import file: Can't open file
This does not appear to be a valid AutoCAD DWG or DXF file
or the file is write protected
--- End Error ---

I am using Sketchup 7 with the optional downloaded DXF/DWG import tool.
0
Last edited by Gruff on Sun Oct 11, 2009 10:52 am, edited 1 time in total.
Resistance is .... Character Forming. *Grin*

Gruff 
 

Re: Automaticaly try to import files that aren't .sku

Postby TIG » Fri Oct 09, 2009 8:35 pm

Your file path refers to .dwg when it's a .dxf ? which might cause problems ?

If it's a simple typo, easy fix...
If not, is it a valid file/path ?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Automaticaly try to import files that aren't .sku

Postby Eric_Erb » Fri Oct 09, 2009 9:56 pm

if i could get it to work with .dxf or .dwg I'd be thrilled. So if one would be easier than the other for some reason, that's fine. Same goes for any other 3D cad format from autoCAD for that matter.
0

Eric_Erb 
 

Re: Automaticaly try to import files that aren't .sku

Postby Gruff » Sun Oct 11, 2009 10:48 am

Eric_Erb wrote:if i could get it to work with .dxf or .dwg I'd be thrilled. So if one would be easier than the other for some reason, that's fine. Same goes for any other 3D cad format from autoCAD for that matter.


If I can get the script to import properly and if the
ArgV method is supported in Sketchup Ruby I see no reason why something couldn't be put together.

TIG:

My bad. I meant to say DWG in my post above.
I can import the same exact DWG file from the same path manually From the SketchUp Main menu "File | Import | Dxf/Dwg"
The Ruby script I show fails every time.

Could you try the import script with a DWG file?

I have the feeling that since they removed DXF/DWG import and made it an addin that the Sketchup ver 7 API no longer supports DXF/DWG import regardless of whether you install the separate importer download.
0
Resistance is .... Character Forming. *Grin*

Gruff 
 

Re: Automaticaly try to import files that aren't .sku

Postby TIG » Sun Oct 11, 2009 12:32 pm

Gruff wrote:
Eric_Erb wrote:if i could get it to work with .dxf or .dwg I'd be thrilled. So if one would be easier than the other for some reason, that's fine. Same goes for any other 3D cad format from autoCAD for that matter.


If I can get the script to import properly and if the
ArgV method is supported in Sketchup Ruby I see no reason why something couldn't be put together.

TIG:

My bad. I meant to say DWG in my post above.
I can import the same exact DWG file from the same path manually From the SketchUp Main menu "File | Import | Dxf/Dwg"
The Ruby script I show fails every time.

Could you try the import script with a DWG file?

I have the feeling that since they removed DXF/DWG import and made it an addin that the Sketchup ver 7 API no longer supports DXF/DWG import regardless of whether you install the separate importer download.


In the 7.1 Pro version imports dwg/dxf OK... just tried it [again]...
Your command is not typed properly... it should be.
Code: Select all
model.import("C:\\AAA\\ACIS Files\\WKT 2424 A4O.dwg",true)

Note the \\ inside double "" to 'escape' the \ special character.
I also use () to separate the argument from the method, BUT that's not compulsory.
You only need true if you want the report screen to show - I'd use false so it's silent.
After the import the dwg/dxf is added at its/the origin, the Tool will then be Move to allow you to adjust it - if you don't want that set another Tool at the end of your code - e.g. Sketchup.send_action("selectSelectionTool:")

Hope this helps...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Automaticaly try to import files that aren't .sku

Postby Gruff » Mon Oct 12, 2009 5:51 pm

TIG:
:oops: Not used to Ruby's Escape char yet. Didn't have one in VB.

Eric_Erb:
Made several attempts at a solution. Ended up by chaining the following tools

1) Windows File Types setting (A process launched when double clicking a file)
2) RubyScriptLauncher.exe (My New Executable)
3) Ruby template file (textbased)
4) SketchUp

The end result is relatively quick and close to what you asked for. The only thing I can think of that might be an issue is that you can only import one file at a time and that it launches a new copy of SketchUp each time you use it.

I found one unexpected bug with my program this morning so I will not be uploading it until tonight when I get home and fix it.

I wrote the program to be non-specific.
It may be used with any file type that Sketchup can import through the SketchUp API.
You control what happens via the information you plug into the Windows File type settings and a the Ruby template file. You can have multiple template files for different import file types. My program uses the template file to construct a ruby file in plugins before starting Sketchup. After Sketchup is running and the file is imported the ruby file is automatically deleted.

Both the program and the template file must be located in the SketchUp PlugIns folder.

--- template file Auto_Import_DWG.TXT example ---
# TEMPORARY_RUBY_FILENAME = Auto_Import_DWG.rb
# The comment line above is required. Use any name you like for the .rb file.

model = Sketchup.active_model
show_summary = false
status = model.import "INSERT_FILENAME_TO_IMPORT_HERE", show_summary

# In the line above the string "INSERT_FILENAME_TO_IMPORT_HERE" must exist.
# It is case sensitive

layers = model.layers

# Turn on all imported Layers
layers.each do | layer |
layer.page_behavior = LAYER_VISIBLE_BY_DEFAULT
layer.visible = true
end

view = model.active_view
view.refresh
view.zoom_extents

# All comment lines and empty lines are removed when the ruby file is created.
# Feel free to add whatever ruby code you want to run.

--- end template file Auto_Import_DWG.TXT example ---

--- Windows File Type Information example ---
Application to perform this action:
"C:\Program Files\Google\Google SketchUp 7\Plugins\RubyScriptLauncher.exe" "%1" "Auto_Import_Dwg.TXT"
(Note the double quotes around each piece of information.)

If you need help getting to and setting up the Windows File Type settings for DWG let me know.
0
Resistance is .... Character Forming. *Grin*

Gruff 
 

Re: Automaticaly try to import files that aren't .sku

Postby Gruff » Tue Oct 13, 2009 11:10 pm

Okay, Here is the final product.

I added a couple more comment line constants for the template file that control.

Seconds until file deleted 2 - 30
Show Launcher Dialog true/false

The Attached Zip file contains three files.
All three need to be placed in your PlugIns Folder.
1) RubyScriptlauncher.exe
2) Auto_Import_dwg.txt (Sample template file)
3) File Type Diagram.jpg (Setting up 'File Type')

Since I did not use any fancy controls you should not need an installation setup program for RubyScriptLauncher.exe

RE: setting up the 'File Type' for DWG or other CAD files.
If you have AutoCad or other applications that use DWG then it could become problimatical.

Have Fun,
0
Last edited by Gruff on Fri Oct 16, 2009 12:49 am, edited 1 time in total.
Resistance is .... Character Forming. *Grin*

Gruff 
 

Re: Automaticaly try to import files that aren't .sku

Postby Eric_Erb » Thu Oct 15, 2009 5:42 pm

Thanks Gruff. I've been out sick for a couple days, but I'm going to try this right now. Thanks again
0

Eric_Erb 
 

Re: Automaticaly try to import files that aren't .sku

Postby Eric_Erb » Fri Oct 16, 2009 4:55 pm

Anybody get this to work?
0

Eric_Erb 
 

Re: Automaticaly try to import files that aren't .sku

Postby Gruff » Fri Oct 16, 2009 6:29 pm

Eric_Erb wrote:Anybody get this to work?

Well obviously I have. :D

What are you having trouble with? Do you get any errors?
0
Resistance is .... Character Forming. *Grin*

Gruff 
 

Re: Automaticaly try to import files that aren't .sku

Postby Eric_Erb » Fri Oct 16, 2009 8:51 pm

I sent you a couple PMs. The first problem I'm having it that I can't set my dwg files to open with your .exe. I can set them to open with anything else but when I select yours it ignores it and stays with whatever program was selected before. The second problem is when I drop a .dwg file on your .exe I get this error...

I'm sure I'm doing something wrong
0

Eric_Erb 
 

Re: Automaticaly try to import files that aren't .sku

Postby Jim » Fri Oct 16, 2009 11:39 pm

Can't use a .bat file and environmental variables (which a ruby can check for the existence of on SU startup?)

Here's a .bat

Code: Select all
set LSU=%1
"c:\Program Files\Google\Google Sketchup 7.1\sketchup.exe"


And from Ruby once SU is running:

Code: Select all
ENV['LSU']=> "C:\Documents and Settings\Jim\My Documents\drawing\100.dxf"


Can you set the file association using a .bat? I would guess so using the reg command. However, I would not recommend changing a user's file association for a .dxf.
0
Hi

Jim 
Global Moderator
 

Re: Automaticaly try to import files that aren't .sku

Postby Gruff » Mon Oct 19, 2009 5:54 am

Eric,

Sorry about that my PM'er is apparently not working.

Re-thought the whole issue. Perhaps I tried to make it too non-specific.
Delete the executable and the template file.

Attached is a one trick pony sort of program in a Zipped file.
Unzip it and put it in your plugins folder. Make a shortcut to it on your desktop.

Run the program and drag-drop a DWG or DXF file anywhere on the program's dialog.

Sketchup will be launched and your DWG / DXF file should be loaded.
(This program will not work by file association at all.)
----

I hard coded the creation of the ruby file inside the executable.
The delay time to file deletion in the dialog is necessary as the Ruby Script file it creates has to be destroyed, otherwise the script would try to import the same file everytime you started SketchUp.

The delay is user definable because you may need to load some very large Autocad files and it wouldn't do to have the Ruby file deleted while it is still trying to import.

The program will remember your last delay time between sessions.
0
Resistance is .... Character Forming. *Grin*

Gruff 
 

Re: Automaticaly try to import files that aren't .sku

Postby Gruff » Mon Oct 19, 2009 9:27 am

Jim could you expand on what you are saying some?

Are you saying the a Windows Environment variable can be read by ruby in sketchup?
If so can it be set to nil when the ruby script is done importing the DWG?

I guess I envision something like the following pseudo-code

Code: Select all
If EnvVar != nil then
  import EnvVar
  set EnvVar = nil
end if


That would be a better idea than creating and destroying a hard coded script file as my program currently does.
0
Resistance is .... Character Forming. *Grin*

Gruff 
 

Re: Automaticaly try to import files that aren't .sku

Postby Eric_Erb » Mon Oct 19, 2009 4:28 pm

Thanks again Gruff... it worked without a hitch. It's a bit of a work around but it works and that's the important thing. I might try to figure out how to launch it during startup and just have it appear down in the notification area of the windows toolbar so I can drag and drop files to it there instead of just keeping a your small window on screen all the time.
0

Eric_Erb 
 

Re: Automaticaly try to import files that aren't .sku

Postby Jim » Mon Oct 19, 2009 4:37 pm

Yes, the environment variables are stored in a hash named ENV. Just type ENV in the Ruby Console:

Code: Select all
ENV # View the entire hash
ENV["TheKey"] # returns a value
ENV["TheKey'] = nil # setting a value


I was thinking the ruby could live permenent;y in the Plugins foder, and just check for the existence of and env variable. If it is set, do to import (perhaps using a messagebox to confirm.) after everything finishes loading.

Gruff wrote:Jim could you expand on what you are saying some?

Are you saying the a Windows Environment variable can be read by ruby in sketchup?
If so can it be set to nil when the ruby script is done importing the DWG?

I guess I envision something like the following pseudo-code

Code: Select all
If EnvVar != nil then
  import EnvVar
  set EnvVar = nil
end if


That would be a better idea than creating and destroying a hard coded script file as my program currently does.
0
Hi

Jim 
Global Moderator
 

Re: Automaticaly try to import files that aren't .sku

Postby Gruff » Tue Oct 20, 2009 12:57 am

[quote="Jim"]
Code: Select all
ENV # View the entire hash
ENV["TheKey"] # returns a value
ENV["TheKey'] = nil # setting a value


Cool Beans Jim. 8-)

Of course it doesn't solve requiring my app to be running all the time. I suppose I could make it so it always stays on top but that could get to be annoying.

Dropping the DWG file on the taskbar icon doesn't Drop the file. Instead it make the application you dropped on the active application (So you have to drop a second time)

Can a web form detect a DragDrop event? If so perhaps a small always running web form would a better route? At least you could then import Acad files while SketchUp was running.

Don't know diddly about web apps so I am out of my element there.
0
Resistance is .... Character Forming. *Grin*

Gruff 
 

Re: Automaticaly try to import files that aren't .sku

Postby Jim » Tue Oct 20, 2009 1:08 am

Gruff wrote:Can a web form detect a DragDrop event? If so perhaps a small always running web form would a better route? At least you could then import Acad files while SketchUp was running.


Great thought, and I'd bet the file path would then be accessible in the dialog and then could passed to Ruby, but I don't know enough about it to even test it.

update: dropping a file onto a dialog does indeed trigger some event. In my test case a Ruby file was dropped on a webdialog, and i was then presented the opportunity to save the file. I'm not sure if/how that could be trapped in JavaScript.

Dropping a text file displayed the file in the dialog.

up-update - couldn't be as simple as an ondrop function?
0
Hi

Jim 
Global Moderator
 

Re: Automaticaly try to import files that aren't .sku

Postby Jim » Tue Oct 20, 2009 2:52 am

Here's an example for using the ondrop. I do not know how to get the dropped file name.

(I really wish this Ruby syntax would work:)

Code: Select all
@dropper_dlg = UI::WebDialog.new
@dropper_dlg.set_html(DATA.read)
@dropper_dlg.add_action_callback("drop") { |d, a|
  puts "drop:#{a.inspect}"
}
@dropper_dlg.show

__END__
<html>
  <head>
    <script>
      function drop() {
        window.location="skp:drop";
      }
      function cancelEvent() {
        window.event.returnValue = false;
      }
    </script>
  </head>
  <body ondragenter="cancelEvent()" ondragover="cancelEvent()" type=text ondrop="drop();">
    Hi There
  </body>
</html>
0
Hi

Jim 
Global Moderator
 

Re: Automaticaly try to import files that aren't .sku

Postby Gruff » Tue Oct 20, 2009 5:23 pm

Jim wrote:I do not know how to get the dropped file name.


In Windows Forms programming the event returns a variable of type DataObject. The DataObject has (Among other things.) a Files collection property
Code: Select all
-- VB Code--
Private Sub OnDragDrop(Data as DataObject, _
                       Effect as Long, _
                       Button as integer, _
                       X as single, _
                       Y as single

  If Data.Files.Count = 1 then
    sDroppedFileName = Data.Files(1)
  end if

End Sub


Perhaps there is a similar structure in Java?
0
Resistance is .... Character Forming. *Grin*

Gruff 
 

Re: Automaticaly try to import files that aren't .sku

Postby Jim » Tue Oct 20, 2009 5:30 pm

My guess is that it is in this dataTransfer object referenced on this page:

http://msdn.microsoft.com/en-us/library ... 85%29.aspx

But I haven't had a chance to try it.
0
Hi

Jim 
Global Moderator
 

Re: Automaticaly try to import files that aren't .sku

Postby Eric_Erb » Tue Oct 20, 2009 7:32 pm

Yeah... I'm going to stay out of this conversation and just try to follow along as best I can. How do you guys think in script? I think my brain was completely maxed out after I added about 50% of AS3 to it, so HTML CSS PHP and AS3. I just don't seem to be retaining ruby like I did the others. Maybe I'm just getting old.
0

Eric_Erb 
 

Re: Automaticaly try to import files that aren't .sku

Postby Gruff » Wed Oct 21, 2009 6:36 pm

Eric_Erb wrote:How do you guys think in script? I just don't seem to be retaining ruby like I did the others.


Not to worry Eric. The old adage "If you don't use it you lose it." applies big time to coding. I write VB Code for a living so I have to keep up on some things.

It is not a mystery to me that Ruby is hard to remember. I put it down to the strict case sensitivity and the lack of a comprehensive Integrated development environment.
0
Resistance is .... Character Forming. *Grin*

Gruff 
 

Re: Automaticaly try to import files that aren't .sku

Postby alexdobras » Fri Feb 25, 2011 6:09 pm

what about a DWG/DXF export plugin for SU free ???
0

alexdobras 
 

Re: Automaticaly try to import files that aren't .sku

Postby TIG » Fri Feb 25, 2011 6:19 pm

alexdobras wrote:what about a DWG/DXF export plugin for SU free ???

There is a separate dwg/dxf exporter available for v7 Free - search here for links.
It's not available for v8 but you can have v7 and v8 Free on your PC at the same time.
There is a dwf exporter under development for v8 Free here too - advance-search for 'honoluludesktop' the author...
0
TIG
User avatar
TIG 
Global Moderator
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 

Next


 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 9 guests

Visit our sponsors: