.add_group causes deleted entity

In my program, the line:

saddle1 = newcomp.entities.add_group

causes “undefined method ‘entities’ for #<Deleted Entity:0x470…….

the line before this one is: puts ““deleted ? = #{newcomp.deleted?}”

That outputs “deleted ? = false”, which makes sense because newcomp was just created. If I don’t try to add a saddle then the whole program runs without error. It looks alot like garbage collection is deleting newcomp just by calling .entities.add_group on it.

SketchUp is very aggressive about deleting empty groups. So, if you create one the longstanding safeguard is to immediately add a cpoint as a placeholder that you delete after you have added the actual content you need.

I don’t get it. newcomp is a component instance fully defined and transformed and it works. I am trying to create a saddle group for it. But newcomp deletes before I can add a single point to saddle.

My chatbot told me to just to add “newcomp.make_unique” but that changed nothing.

I think we need to see more of the surrounding code to figure out what is happening.

Here is a large excerpt. More available if necessary:

side_noz_array=\["tank child","Pipe","NozzleTankSide",@scope,@wbs,@system,"S1","","","",

                "bottom outlet nozzle","","","SS02","OUT","","","","","",90,3,"Radial",0,"",

                "","",side_noz_base_pt.x/12.0,side_noz_base_pt.y/12.0,

                side_noz_base_pt.z/12.0,

                0,0,3,"ANSI",8.0,false,"","",0,"","",@equipID,""\]

@data_array\[42\] << side_noz_array  

top_noz_base_pt=Geom::Point3d.new(0,tankRadius,@tankBaseHeight+@tankSS-3)

top_noz = ent.add_group()



top_noz_array =  \["tank child","Pipe","NozzleTankHead",@scope,@wbs,@system,"B","","","",

                "top center nozzle","","","SS02","OUT","","","","","",0,0,"VERTICAL",0,"",

                "","", top_noz_base_pt.x/12.0, top_noz_base_pt.y/12.0,

                top_noz_base_pt.z/12.0,

                0,90,3,"ANSI",8.0,false,"","",0,"","",@equipID,""\]

@data_array\[42\] << top_noz_array  

heightAdjustment=50

ent.erase_entities(tankcircle)

@groundClearance=24

@supportSpacingFactor=50.0  # zero to 100

supportSpacing=(@supportSpacingFactor/100.0)\*(@tankSS-4)/2.0

tp=Geom::Transformation.new(\[@x,@y,@z+@tankDiameter+24\])

tr1=Geom::Transformation.rotation(ORIGIN,Z_AXIS,@orientation.degrees)

tr2=Geom::Transformation.rotation(ORIGIN,X_AXIS,90.degrees)

newcomp=this_model.active_entities.add_instance(new_comp_def,tp\*tr2\*tr1)

newcomp.make_unique

puts "deleted? #{newcomp.deleted?}   nc = #{newcomp}"

saddle1 = newcomp.entities.add_group()

make_tank_saddle(saddle1, tankRadius, saddleWidth, saddleHeight, saddleLength)

If newcomp is a Sketchup::ComponentInstance object, it does not have an entities method. Only the model litself and Sketchup::ComponentDefinition objects have an Entities collection.

You really need to call:

saddle1 = newcomp.definition.entities.add_group()

.. OR …

saddle1 = new_comp_def.entities.add_group()

Prevent the group from being cleaned up:

saddle1.definition.entities.add_cpoint(ORIGIN)

… and delete the line making newcomp unique.

Thanks Dan. This answer is clear and accurate, the opposite of what I got from my chatbot. AI is good at data mining but bad at computer programming. In my own defense, I suggest that the error message is misleading. newcomp is not deleted, it just has no entities method.

I wondered why you weren’t seeing that exception, which is why I requested a larger code context. I wasn’t sure what the thing you were calling a component really was.

Your experience fits with others I have helped: AI today generates usable code a lot of the time, but when it goes astray a non-programmer doesn’t know how to fix the errors. It often takes a long session of refining the request to get AI back on track.

Yes, I see now that I was not including quite enough code. I was assuming that everyone in the world knows what a “newcomp” is. My bad.

Agreed. It should have output something like:
#<NoMethodError: undefined method ‘entities’ for #<Sketchup::ComponentInstance:0x470…>>"

I did see the line that created the object:

newcomp = this_model.active_entities.add_instance(new_comp_def,tp\*tr2\*tr1)

BTW, it does not save the interpreter much work by collapsing spaces and it makes code much harder to read. Most code style guides say to always have a space on each side of an operator.