Attempting to load a component file from my disk via ruby script file I receive an Error: #<IOError: Invalid component file>

Hi, I’m attempting to import an .skp file that was saved via SaveAs by right clicking on the component, but when I go to retrieve the file, it requires that I use the mouse and click where I want to set the component. I want to automate this process by preselecting a point, and setting it to that point in my model. From my research, I learned the only way to do this is to use the DefinitionsList.load method. I’m loading directly from my disk and not from a url.

Here are the lines of code. Note: to shorten the file I have described the path dir within {}

x_file = "D:/{MyRootDir}/{MyComponentsDir}/810 Lumber/03 CustomCuts/01 Ply3_4x4in/002 EndRollerInnerSupportDisc"

objModel = Sketchup.active_model
objImportedDef = objModel.definitions.load(x_file)

These 2 lines work perfectly from the Ruby console. However, when I run the script, I receive the following.

Error: #<IOError: Invalid component file>
D:/{MyRootDir}/{MyComponentsDir}/aa SketchupUtilities/aa ProjectScriptsLib/sjnUI_GroupUtilities.rb:164:in `load'

I can’t find any source that helps me to understand why the same line of code which I copy from my script file which is failing, but I can copy the exact line of code and paste on the console, works perfectly.

Can you help me with this issue?

Thanks,

Scott.

Does the pathname have the “.skp” extension on the end of it ?


Please quote code properly in the forum … [How to] Post correctly formatted and colorized code on the forum?

It is smart to test pathnames for validity thus …

if File.exist?(x_file)
  # attempt to load it
else
  # notify user and bailout
end

Yes, I just failed to copy the entire filename with the extension.

File.exist?(x_file)
true

I also did that to verify that the transfer of the file name was correct before executing the method to import/load.

The contrast between behavior in the Ruby Console and from your script suggests that something earlier in the script is interfering. I’d try the old-fashioned way and

puts x_file 

just before the load.

Can you share a sample model for testing? Does it happen on all files you try to load, or just some?

What version of SketchUp are you using? Have you checked what version the SKP was saved in?

1 Like

Can you explain to me how I can create those windows within this reply to show you the code and possibly the screen prints. That would allow you to have the same view as I have.

From the console this is the version of Sketchup

Sketchup.version
18.0.16975
Sketchup.version_number
1800016975

Here’s the actual code:

(Note: I use the first line to test the method from the console by first creating the variables that need to be passed.)

#SJN::MyGroupUtilities.importFile(x_file, x_sortkey)
def self.importFile(x_file, x_sortKey)
			
puts "FileName: #{x_file}"
UI.messagebox("FileName: #{x_file}")
UI.messagebox("SortKey : #{x_sortKey}")

objModel = Sketchup.active_model
			
objModel.definitions.purge_unused
puts "purged unused definitions..."
UI.messagebox("purged unused definitions...")

objImportedDef = objModel.definitions.load(x_file)
puts "Loaded selected file to definition..."
UI.messagebox("Loaded selected file to definition...")

objNewInstance = objModel.active_entities.add_instance(objImportedDef, [0,0,0])
puts "added objNewInstance #{objNewInstance.definition.name}"
UI.messagebox("added objNewInstance #{objNewInstance.definition.name}")

objSelection.add objNewInstance
puts "Selected New Instance..."
UI.messagebox("Selected New Instance...")

objWrapper = objModel.active_entities.add_group(objNewInstance).to_component
puts "Created WrapperComponent..."
UI.messagebox("Created WrapperComponent...")

objSelection.add objWrapper
puts "Selected WrapperComponent..."
UI.messagebox("Selected WrapperComponent...")

objSelection[0].explode
puts "Exploded Selected object..."
UI.messagebox("Exploded Selected object...")

objSelection.clear
			
objSelection.add objModel.active_entities[-1]
puts "Selected last created object instance..."
UI.messagebox("Selected last created object instance...")

objSelection[0].name = x_sortKey
puts "Applied Sort Key to Imported Component..."
UI.messagebox("Applied Sort Key to Imported Component...")

end

When I run the program here’s the response from the console.

myJSLink: htmlCmd => Exe:Import
myJSLink: htmlSource => D:/aa SJN Service Enterprise/Business Projects/aa SketchupUtilities/aa ProjectComponents/810 Lumber/03 CustomCuts/01 Ply3_4x4in/002 EndRollerInnerSupportDisc|ci.810.03.01.
myJSLink: htmlTag => *
myJSLink: calling process_jsRequests...
SJN::MyHTMLUtilities: htmlCmd = Exe:Import
SJN::MyHTMLUtilities: htmlSource = D:/aa SJN Service Enterprise/Business Projects/aa SketchupUtilities/aa ProjectComponents/810 Lumber/03 CustomCuts/01 Ply3_4x4in/002 EndRollerInnerSupportDisc|ci.810.03.01.
SJN::MyHTMLUtilities: htmlTag: *
Exe:Import: D:/aa SJN Service Enterprise/Business Projects/aa SketchupUtilities/aa ProjectComponents/810 Lumber/03 CustomCuts/01 Ply3_4x4in/002 EndRollerInnerSupportDisc|ci.810.03.01.
FileName: D:/aa SJN Service Enterprise/Business Projects/aa SketchupUtilities/aa ProjectComponents/810 Lumber/03 CustomCuts/01 Ply3_4x4in/002 EndRollerInnerSupportDisc
purged unused definitions...
Error: #<IOError: Invalid component file>
D:/aa SJN Service Enterprise/Business Projects/aa SketchupUtilities/aa ProjectScriptsLib/sjnUI_GroupUtilities.rb:163:in `load'
D:/aa SJN Service Enterprise/Business Projects/aa SketchupUtilities/aa ProjectScriptsLib/sjnUI_GroupUtilities.rb:163:in `importFile'
D:/aa SJN Service Enterprise/Business Projects/aa SketchupUtilities/aa ProjectScriptsLib/sjnUIUtils.rb:318:in `cmdExe'
D:/aa SJN Service Enterprise/Business Projects/aa SketchupUtilities/aa ProjectScriptsLib/sjnUIUtils.rb:55:in `process_jsRequests'
D:/aa SJN Service Enterprise/Business Projects/aa SketchupUtilities/aa ProjectScriptsLib/sjnUIUtils.rb:966:in `block in show_dialog'
SketchUp:1:in `call'

As you can see it fails on the .load (4th command down)

But copying and pasting the same lines of code from the method and using the console, this is the response

objModel = Sketchup.active_model
#<Sketchup::Model:0x00026f3d8f4810>
objModel.definitions.purge_unused
#<Sketchup::DefinitionList:0x00026f3d830b68>
objImportedDef = objModel.definitions.load(x_file)
#<Sketchup::ComponentDefinition:0x00026f3e209ce8>
objNewInstance = objModel.active_entities.add_instance(objImportedDef, [0,0,0])
#<Sketchup::ComponentInstance:0x00026f3e209338>
objSelection.add objNewInstance
1
objWrapper = objModel.active_entities.add_group(objNewInstance).to_component
#<Sketchup::ComponentInstance:0x00026f3e1fbeb8>
objSelection.add objWrapper
1
objSelection[0].explode
[#<Sketchup::AttributeDictionary:0x00026f3e1fb760>, #<Sketchup::ComponentInstance:0x00026f3e1fb738>, #<Sketchup::AttributeDictionaries:0x00026f3e1fb710>]
objSelection.clear

objSelection.add objModel.active_entities[-1]
1
objSelection[0].name = x_sortKey
SortKey 000

Everything goes through without a hitch. It took me a week to perfect this automated process and from the console it works perfectly. The only other option I have is to split the process into 3 sections and require the user (me) to manually use the cursor on the import and place it anywhere on the model, and then apply the sort key to component name. That becomes a 3 step process.

Thanks for you interest and response. This has been really frustrating to get so close and then have things not function properly when I can prove that the code works.

Scott.

I think Dan took the time to write it in the hope you would see and read it… By the way, you can edit posts (so no need to post again).

1 Like

After further review of the process in transferring the file name, and per your suggestions, I discovered that there was an error in the process of the filename not having the .skp extension and it was my fault. I’m really sorry for bothering you folks to assist me in a problem that I created, and apparently was easily resolved.

Thanks so much. Again I’m really sorry to trouble, and taking up your time.

Scott.

1 Like

The “suggestion” came from being there and seeing that. (1st hand experience.)

However, it would be nice if the API docs would publish the exception reasons for API methods (especially those that do IO.)

We don’t mind because easily answered questions make us look like miracle workers. :wink:

1 Like