Is it possible to add component Attributes in SketchUp SDK, without saving it?

When I try to add component attributes by SketchUp SDK, I need to save the model to made changes in the active document. Model saving takes more time. Without saving model, how to add component attributes by SketchUp SDK?

For Modify Attributes
SU_RESULT SUEntityAddAttributeDictionar(SUEntityRef entity,const char * name,SUAttributeDictionaryRef * dictionary)
[https://extensions.sketchup.com/developer_center/sketchup_c_api/sketchup/entity_8h_source.html]

For Save Model
SU_RESULT SUModelSaveToFile(SUModelRef model, const char* file_path);
[https://extensions.sketchup.com/developer_center/sketchup_c_api/sketchup/model_8h_source.html]

Any changes made to a model needs to be saved. I’m not sure how you’d otherwise expect the changes to be persisted?

2 Likes

I expect like Ruby API. I tried to add attributes using Ruby it directly reflect in active model, without saving the model.

Can you provide a complete minimal example using the C API that illustrates what you are seeing? (Something that could be pasted into main of a CLI application and run.)

//We get active model path from Rupy API		
modelPath = model_filename;
SUInitialize();
model_ = SU_INVALID;
SUResult res = SUModelCreateFromFile(&model_, model_filename.c_str());
if (res != SU_ERROR_NONE)
{

}
else
{
	//We get attributes data from the XML file
	
	SUEntitiesRef entities = SU_INVALID;
	SUModelGetEntities(model_, &entities);
		
SU_RESULT SUEntityAddAttributeDictionary(SUEntityRef entity,const char * name,SUAttributeDictionaryRef * dictionary)
		
	//Component Attributes are stored in the model only after executing the below methods
		
	sketchUpInterop->SaveandCloseActiveModel(); // Save the model in Rupy API and Close it
	res = SUModelSaveToFile(model_, modelPath.c_str());// Save the model in SketchUp SDK
	sketchUpInterop->OpenActiveModel(modelPath.c_str());// Again open the model by Rupy API
}

There is any way to access the active model by SketchUp SDK?

Ah - so that is the core of this question? You have a Ruby Extension and want to use the C API to access the open model?

There is a way - but it’s read only. You cannot write to the model. So any model modifications must be done using the Ruby API.

@tt_su Yeah, you are right.

We try to add attributes using Ruby API which takes more time than C API.
Is there any way other than this?

No - if you modify the model from a SketchUp extension you must use the Ruby API.

Can you provide a example code snippet that demonstrate the performance you observer in the Ruby API? One that we can paste into the Ruby Console and run.

BTW it looks like the SUEntityAddAttributeDictionary function is misspelled in the above example.

@tt_su

Please refer below code snippet,

def  SaveElementID()
		model = Sketchup.active_model	
		defnHash = Hash.new			
		model.definitions.each{|defin|
		    instanceHash = Hash.new		
			defin.instances.each{|inst|			 
				 SaveInstanceElementID(inst,"InstanceID",instanceHash)				
				  }				 
				 SaveInstanceElementID(defin,"TypeID",defnHash)				
			 }
end

def SaveInstanceElementID(inst,attname,hash)	  
	     attrdict = inst.get_attribute("StoredID",attname,"")		
		 if(attrdict == nil ||attrdict=="")		
			 uuid=SecureRandom.uuid
			 inst.set_attribute("StoredID",attname,uuid)			
			 hash[uuid]=inst.guid
		elsif(hash[attrdict]==nil)			 
			 hash[attrdict]=inst.guid
		else		
			uuid=SecureRandom.uuid
		    inst.set_attribute("StoredID",attname,uuid)		   
			hash[uuid]=inst.guid
	    end		
end

@thomthom Wasn’t there problems prior to Ruby 2.5 with SecureRandom.uuid using the bugged OpenSSL library ?

It was a performance issue. SecureRandom uses OpenSSL to generate random bits. And prior to SU2019 and Ruby 2.5 the OpenSSL version Ruby used was extremely slow under Windows. You wouldn’t notice it too much on a fresh start - but the more memory the SketchUp process had consumed the slower it would take. We’re talking from several seconds to minutes.

This would happen only on initialisation. So if OpenSSL is used early on an empty model upon startup you probably avoid the worst of it. But opening a SketchUp with a bit model and you’re stuck with a spinning busy cursor.

Got a sample model to go along with this?

@tt_su
We can’t able to share the model since it is confidential. But I can mention the information that was available in the model,

  • 2730 Component Definitions
  • 630 Group Definitions
  • 16548 Instance

You are trying to store an uuid, i guess to make it possible to, well, identify elements in a later stage… why not use ‘entity.persistent_id’ ?

Also, you could half the number of writes to the model. You are setting an attribute on a definition on each iteration of its instances.
Edit: oops, it seems you are not. Guess i was confused about the indention.

Finally, have you tried using operations to write your attributes in ruby? Doing all these writes in a single operation should speed things up.

2 Likes

Anything you can share with us in private?