Loading Components from a File (Part II)

I’m getting some bugsplats (rather unpredictably) when running this chunk of code:

def load_component (anchorbolt, nuttype, washertype)

	current_time = Time.now
	formatted_time = current_time.strftime("%Y%m%d%H%M%S")

	model = Sketchup.active_model
	definitions = model.definitions

	this_dir=File.dirname(__FILE__)
	# Fix for ruby 2.0
	if this_dir.respond_to?(:force_encoding)
		this_dir=this_dir.dup.force_encoding("UTF-8")
	end

	filename1 = File.join(this_dir,"components/#{anchorbolt}.skp")		
  	@componentdefinition = definitions.load(filename1)
	@componentdefinition.name = "#{@Anchorbolt}_#{formatted_time}"

	filename2 = File.join(this_dir,"components/#{nuttype}.skp")		
  	@componentdefinition_nut = definitions.load(filename2)

	filename3 = File.join(this_dir,"components/#{washertype}.skp")		
  	@componentdefinition_washer = definitions.load(filename3)
end

About a year ago Eneroth made some mention of the problems with loading components from files in this thread:

I think this is what I am running into here, and it is very frustrating.

After I call this method and load the three components (files), I then modify the main component by dropping the geometry from the nut and washer components into it.

Here is the main anchor bolt method that loads the components (via the method shown above) and then modifies the main component definition:

MedeekMethods.load_component @Anchorbolt, @Nuttype, @Washertype


	################################################################
	#
	# Nut and Washer Components inserted with Anchor Bolt Component
	#
	################################################################

	if @Curboption == "YES"
		ztrans = @Boltlength - 7.0 + @Curbheight
	else
		ztrans = @Boltlength - 7.0 
	end

	ztrans_washer = @Sillthickness - (@Boltlength - 7.0)
	ztrans_nut = @Washerthickness + @Sillthickness - (@Boltlength - 7.0)

	transformation_washer = Geom::Transformation.new([0,0,ztrans_washer])
 	componentinstance_washer = @componentdefinition.entities.add_instance(@componentdefinition_washer, transformation_washer)
	array_washer = componentinstance_washer.explode

	transformation_nut = Geom::Transformation.new([0,0,ztrans_nut])
 	componentinstance_nut = @componentdefinition.entities.add_instance(@componentdefinition_nut, transformation_nut)
	array_nut = componentinstance_nut.explode


	##############
	#
	# X-Dir Bolts
	#
	##############


	xtrans = @Cornerdist
	ytransfront = @Sillwidth/2.0
	ytransback = @BuildingLength - @Sillwidth/2.0

	trans_abolt = Geom::Transformation.new([xtrans, ytransfront, ztrans])
 	aboltinstance1 = Sketchup.active_model.active_entities.add_instance(@componentdefinition, trans_abolt)
	aboltinstance1.name = "Anchor Bolt 1F"

The problem is I have no idea on how to deal with this instability. My only solution would be to convert the components to groups and then purge the components from the model so that when the plugin reloads them again it will perform the task in a predictable manner.

I’m noticing that the plugin does not crash when re-loading the nut or the washer components. These components do not get modified so I’m not sure exactly how it is handled but they do not appear to be the problem.

If I specify a different sized anchor bolt then a different file is loaded and there is no problem, the bug splat only happens when the plugin tries to reload the same file or component and enters some sort of state of confusion, or something.

SketchUp version ?

Crash Report Number ?

Identifying text sent with the splat report so it might be looked up?


Common things to do or check:

Verify the validity of the definition reference after the load call, before calling methods upon it.

rescue clause(s) in the crashing method to try and catch an exception and prevent the splat?

File versions of each of the components ?
(Loading newer component versions into older application versions cause splat prior to ver 2019.0)

check each component file for errors ?


Did you read the documentation for DefinitionList#load with regard to failure ? IE …

Raises:

  • RuntimeError If filename is an invalid SketchUp model. Added in SketchUp 2019.
    Possible that prior versions of SketchUp will crash.

(Emphasis by Dan.)

1 Like

The file version is purposely SU 2015 so as to remain compatible with that version of SU or greater.

After some further testing I think the editing of the main anchor bolt component is the issue. I can probably avoid the issue by creating the overall component from scratch each time and then dropping the other components into this main component so that the file or loaded components never actually get modified by the plugin. For some reason when the component is modified and then the plugin attempts to reload this component from a file it becomes unstable. I’m really not understanding what is going on here but I will do my best to come up with a work around.

Here is the updated block of code. It appears to fix the problem (no more bug splats, Yay!):

MedeekMethods.load_component @Anchorbolt, @Nuttype, @Washertype


	################################################################
	#
	# Nut and Washer Components inserted with Anchor Bolt Component
	#
	################################################################

	if @Curboption == "YES"
		ztrans = @Boltlength - 7.0 + @Curbheight
	else
		ztrans = @Boltlength - 7.0 
	end

	ztrans_washer = @Sillthickness - (@Boltlength - 7.0)
	ztrans_nut = @Washerthickness + @Sillthickness - (@Boltlength - 7.0)

	current_time = Time.now
	formatted_time = current_time.strftime("%Y%m%d%H%M%S")

	@ab_component = Sketchup.active_model.definitions.add "#{@Anchorbolt}_#{formatted_time}"
	
	transformation_abolt = Geom::Transformation.new([0,0,0])
 	componentinstance_bolt = @ab_component.entities.add_instance(@componentdefinition, transformation_abolt)
	array_bolt = componentinstance_bolt.explode

	transformation_washer = Geom::Transformation.new([0,0,ztrans_washer])
 	componentinstance_washer = @ab_component.entities.add_instance(@componentdefinition_washer, transformation_washer)
	array_washer = componentinstance_washer.explode

	transformation_nut = Geom::Transformation.new([0,0,ztrans_nut])
 	componentinstance_nut = @ab_component.entities.add_instance(@componentdefinition_nut, transformation_nut)
	array_nut = componentinstance_nut.explode
	

	##############
	#
	# X-Dir Bolts
	#
	##############


	xtrans = @Cornerdist
	ytransfront = @Sillwidth/2.0
	ytransback = @BuildingLength - @Sillwidth/2.0

	trans_abolt = Geom::Transformation.new([xtrans, ytransfront, ztrans])
 	aboltinstance1 = Sketchup.active_model.active_entities.add_instance(@ab_component, trans_abolt)
	aboltinstance1.name = "Anchor Bolt 1F"

Each time the foundation is edited a new anchor bolt component definition is created which is not particularly desirable but at least it is no longer is crashing SketchUp.

The only way I’ve seen DefinitonsList#load crash SketchUp is when loading a component made in a newer SketchUp version. The other unexpected behavior I’ve seen has been related to overriding existing definitions or not having the file loaded at all as SketchUp thought an existing component was identical.

1 Like

Again …

… and if you have reproducible components and code that causes a splat, please file it along with file attachments in the public API issue tracker. It can become part of the test suite to prevent regression bugs even if it might be fixed in recent version(s).

1 Like

I can’t quite put my finger on it but the problem seems to be with reloading the component after it has been edited by the plugin.

My new method does not attempt to “edit” any loaded component but rather creates a new container component where I simply drop the loaded components into and then explode them into a simple group within this container component. This appears to have eliminated all of the instability.