Script is hanging?

require ‘sketchup.rb’

Directory containing SketchUp models

input_directory_path = “D:/R-and-D/Sketchup”

Directory to save the exported DXF files

output_directory_path = “D:/ModelDFXImages”

Function to save view as a 2D DXF file

def save_2d_dxf(model, view, filename)
options = {
:export_hidden_geometry => true,
:projection => “Parallel”
}
model.export(filename, options)
end

begin

Close all open models

Sketchup.active_model.close(true) while Sketchup.active_model

Iterate through all .skp files in the input directory

Dir.glob(File.join(input_directory_path, “*.skp”)).each do |file_path|
# Open the SketchUp model
model = Sketchup.open_file(file_path)
next unless model # Skip if the file could not be opened

# Create a front view
model.active_view.camera = model.camera.clone.set([0, -1000, 0], [0, 0, 0], [0, 0, 1])
model.active_view.zoom_extents
front_dxf_path = File.join(output_directory_path, "#{File.basename(file_path, ".skp")}_Front_View.dxf")
save_2d_dxf(model, model.active_view, front_dxf_path)

# Create a top view
model.active_view.camera = model.camera.clone.set([0, 0, 1000], [0, 0, 0], [0, 1, 0])
model.active_view.zoom_extents
top_dxf_path = File.join(output_directory_path, "#{File.basename(file_path, ".skp")}_Top_View.dxf")
save_2d_dxf(model, model.active_view, top_dxf_path)

# Close the model without saving changes
model.close

end

puts “2D DXF files have been saved for all models in the specified directory.”
rescue => e
puts “An error occurred: #{e.message}”
puts e.backtrace
end
Faucet_Kitchen 1.skp (464.6 KB)

It would be great help if someone can tell me what is causing issue ?

It would help if you post code in the forum correctly …

require 'sketchup.rb'

input_directory_path = "C://R-and-D//Sketchup"
output_directory_path = "D://ModelDFXImages"


def save_2d_dxf(view, filename)
  options = {
    :export_hidden_geometry => true,
    :projection => "Parallel"
  }
  model = view.model
  result = model.export(filename, options)
  if result
    puts "DXF exported: #{filename}"
  else
    puts "Failed to export DXF: #{filename}"
  end
  return result
end


Sketchup.active_model.close(true) while Sketchup.active_model


skp_files = Dir.glob(File.join(input_directory_path, "*.skp"))
if skp_files.empty?
  puts "No SketchUp files found in the input directory."
else
  skp_files.each do |file_path|
    begin
 
      model = Sketchup.open_file(file_path)
      next unless model # Skip if the file could not be opened
      
      view = model.active_view

   
      pages = model.pages
      front_scene = pages.add("Front View")
      top_scene = pages.add("Top View")

   
      front_camera = Sketchup::Camera.new([0, -1000, 0], [0, 0, 0], [0, 0, 1])
      model.active_view.camera = front_camera
      model.active_view.zoom_extents
      front_dxf_path = File.join(output_directory_path, "#{File.basename(file_path, ".skp")}_Front_View.dxf")
      save_2d_dxf(view, front_dxf_path)

     
      top_camera = Sketchup::Camera.new([0, 0, 1000], [0, 0, 0], [0, 1, 0])
      model.active_view.camera = top_camera
      model.active_view.zoom_extents
      top_dxf_path = File.join(output_directory_path, "#{File.basename(file_path, ".skp")}_Top_View.dxf")
      save_2d_dxf(view, top_dxf_path)

     
      Sketchup.close_active_model
    rescue => e
      puts "Error processing file: #{file_path}"
      puts e.message
      puts e.backtrace.inspect
    end
  end
end

puts "Script execution complete."

(1) Probably you are putting the API into an endless loop with …

  • Sketchup.active_model.close(true) while Sketchup.active_model
    

(2) SketchUp native 2D DWG or DXF export is not exposed to the Ruby API.


(3) Path strings do not use double forward slash characters. Single slash only.
Or double backslash (which is an escaped backslash.)

To get a file directory … usually we use UI.select_directory

AND

(4) Please do not define local variables or methods in the top-level ObjectSpace, which actually within a special instance of Object


(5) These are not valid export options for any file format. See the documentation:


(6) This may not work well the way it is written.

It will show a modal error message box if the file cannot be found or perhaps an exception. So, file IO operations should normally be wrapped up in a beginrescueelseend block.

You should also use the newer overload to prevent deprecated warnings output to console …

      status = Sketchup.open_file(file_path, with_status: true)

SketchUp will try to always have a valid model object but this method does not return a model reference. So, you will need to get the model reference if no error occurred:

    begin
      status = Sketchup.open_file(file_path, with_status: true)
    rescue => error
      puts %[Error opening file "#{file_path}"]
      puts error.inspect
      next # skip to next file
    else
      model = Sketchup.active_model
    end

(7) The Sketchup module does not have a method named close_active_model !

… perhaps the AI meant:

      Sketchup.active_model.close

… but, your code made changes to the model, so the user would get a modal message box asking whether to save changes. To bypass this message box and discard changes, use …

      discard_changes = true
      Sketchup.active_model.close(discard_changes)

(8) Your code added scene pages to the model (without first checking if those pages exist.) Then the code did not ensure that the proper page was activated before changing the camera.

Adding a page will automatically make it the selected_page. So, adding the top_scene should be placed after setting the front_camera and just before setting the top_camera.


(9) Your code did not set either camera’s perspective type:

      front_camera.perspective = false

(10) Ask yourself is it really necessary to even create new scene pages just to export a view with a certain camera setting ?

3 Likes

Thanks Dan! for your suggestions.