Deleted entity error

hello everyone,
i created a face in a sketchup model using the code below:

face = entities.add_face pts
face.edges.each {
|edge| edge.find_faces
}

and then i intersect that face with another existing face in my model using :

 recurse = false
                         hidden = true
                    inter=Sketchup.active_model.entities.intersect_with(
                            recurse,
                            IDENTITY, # Transformation for the calling Sketchup::Entities
                            Sketchup.active_model.entities, # parent (target) Sketchup::Entities
                            IDENTITY, # parent (target) transform
                            hidden,
                           Sketchup.active_model.entities.to_a # Entities to intersect the calling Sketchup::Entities with
                          )

now when i do :

UI.messagebox("face created is  #{face}") 

it shows the error below (the created face from the message is the one with red edges):

I don’t know why it refers to it as deleted entity, when i didn’t delete anything just used the intersection.
Does any one have an idea why ?
thnx

It is dangerous to retain a Ruby reference to a SketchUp Entity across an operation that may modify that entity. Beneath the covers, SketchUp feels free to delete Entities and redraw identical ones as necessary to generate the final geometry. It also may reuse an existing Entity for some part of new geometry. For example, if a Face gets split into pieces, SketchUp may keep the original Face Entity to use as one of the new (smaller) Faces and create new Faces for the rest.

1 Like

i used this same method in another code and it works every time but when i organized my code and divided it into methods it doesn’t work now.
why did it work before but not now also is it possible to keep pointing at that entity ?

SketchUp’s internal decisions about whether to retain vs delete an Entity (and when) are undocumented and sensitive to fine details. It’s a bit like saying “I walked across this street yesterday and didn’t get hit by a car. Why did I get killed today?”. It was a bad idea in any case, but you just got lucky the first time.

2 Likes

@slbaumgartner also it worked for another model just now, mm i used the reference to get the intersection edges created after intersect_with, and until now it still helps getting them for some models.
Is there any way to retrieve the intersection edges ? as the intersect_with method doesn’t return them ?

The Ruby API documentation is in error: intersect_with returns an Array of the Edges it creates. If there are none, it returns an empty Array. It never returns nil!

1 Like

OMG ! and i spent a while trying to get those edges wondering why the intersect_with doesn’t return them, thank you so much for your answer

hello @slbaumgartner i tried the returned values by intersect_with as you said but for the model below, there is only one intersection edge but in the messagebox it shows that it returns 3 edges and sometimes 2 edges for the same model.
so my question is does this function returns the intersection edges created or even the edges that were changed ? and why it shows 3 edges instead of 1 ?
thnx

Your ‘roof’ plane touches the ‘wall’ = 1 edge intersected [same size as it was]
It also cuts another roof plane = 1 new edge at ‘hip’
It also splits its top edge where the ‘hip’ touches = 2 new edges

That adds up to 4 edges resulting from the intersection…

1 Like

but each time it shows different number of edges ? for example in the image bellow there should be an edge that has 4 faces (ones showed in the image) which is the one showed by the arrow, but when i test it shows 2 edges sometimes and sometimes it shows 3 edges but none of them has 4 faces ?

When done manually there are 4 faces for that edge…


Is your code intersecting everything properly, or is it making two coincident edges ?

1 Like

i did zoom that part and i found that even if i draw an edge manually it devids the edge into 2 parts as in the image below.
i don’t know this happens even if i draw it manually from point to point ?


which causes this problem when drawing the face.

this is my model :

testing drawing models.skp (97.0 KB)

This is because your #base is poorly drawn.
If you don’t want a short edge then fix it.
Use a Style with endpoints so it’s easier to see,
I also added a short cline to each edges start so I could see what there are 8 edges when I can see only 7 !
Use the Move tool to relocate the rogue vertex so the two edges merge into one.

This issue has got nothing to do with your coding attempts.
The poorly form base simply needs fixing…


1 Like

@TIG oh oky but i draw the base using a ruby plugin i’m working on so is there a way to merge those edges into one using ruby code ?

Your ruby code must define 8 vertices, not the expected 7.
It needs fixing at that level, before getting to the roof intersection phase…

You can merge colinear vertices, but not having them would be easier !
Fredo has a tool to do this…

To find colinear vertices you need to collect the edges you have made for the ‘base’.
I’d use a hash, referencing each edge and taking its value as its edge.line
Then step through the edges and take each start point [from its line or start.position] and use distance_to_line stepping through each of the other edge’s line - if it’s == 0 then the tested edge’s start is on that line - if the line’s vector == edge’s line’s vector then the two edges are colinear.
Find the shortest edge.
The vertex shared with the other colinear edge can be found.
Make a transformation - translation - using the vector from that shared vertex position to the shortest edge’s other vertex.position; now use transform_entities to transform the shared vertex by the translation and the two edges will merge.
Of course you need to reduce the tested edges as you go to avoid looking at now non-existent entities etc…

As I said it’s best to draw it right rather than fix it later !!

1 Like

@TIG thank you for your answer, yes i agree with you so i tried to find the origin of the problem but the only thing that i found and that may lead to that problem, is that I calculate the vertices values using user inputs in meter, and when i need to convert the result of the calculation i multiply by 39,37 instead of using the to_inch method, as it didn’t work. When i try variable.to_inch it keeps the same value of the variable without conversion.
same fo the to_m method, for example the result of this code (point is a Point3D):

p=point.x/39.37
   UI.messagebox("point to x is by #{point.x} the p value is #{p}")

can this lead to the problem i posted ? also why does the to_inch method not work for a calculation result ?
thnx a lot

The Ruby API docs seem to say that to_inch is a stupid no-action method!

Why not get the user’s input as ‘length’ ?
e.g.

x = 0.m

Then if they input a value in their current units [anything! == m/cm/mm/inch/feet etc] OR append a units suffx - so if they are working in mm, typing 1m assumes 1m etc…

You are making your life far more complicated than it needs to be !

If you are expecting an input ‘point’ in the form x,y,z
then default it as a string "0,0,0"
Why worry about units at all ?
Let the user specify their own units…
Split at the ‘,’ and then convert to length ?

point=str.split(',') px=point.x.to_l

returns the enter value as a ‘length’ - allowing the user to work in any units they like, if they want to work in ‘m’ while set to ‘mm’ they simply add a ‘m’ after the number…

To reiterate: you are overthinking this !

@TIG actually the user will only use meter but that’s not the problem, i need to take the length in meters (width and heigth ) of the face then draw it that’s why i need to convert it to inch to represent the vertices of the face, as when i draw the face using the values entered by the user it draws verry small shapes.