Programmatically creating Geometry problems

I still think it’s wrong to try and use SketchUp like an Etch-a-Sketch …

Personally, I don’t think that this is a bug. Using a set of points that don’t create a properly-defined face can only lead to unexpected results.

That is correct - SketchUp will attempt to merge and split overlapping geometry.

May I ask why you try to create faces like this? Is it part of imported data? Might be worth that we briefly talk about this so we are all on the same page. I’ve seen quite a few integration attempts with SketchUp where things have gotten complicated because one attempted to do thing in a manner familiar to another application - but which isn’t how one would ideally do it in SketchUp.

I am just importing geometry exported by another program, it spits out the face in those 10 vertices; although if I can get a solid pattern of how and why this happens I can maybe make a special case about it. What’s weird is that if I push the object a bit back I can create the face as is no problem… (you can see that in the other attached scene)

hmm… maybe there is an tolerance issue in play here…

@thomthom any clue on what might that be? How can I push for a Sketchup developer to look at this? :slight_smile:

Can you provide example to when the object is pushed back - where it works? As a comparison to when it doesn’t work?

@thomthom The second included file in the original post (link again here) demonstrates exactly this.You can also easily do that with the provided problematic scene if you take the object in it and move it back a couple of points in the x-axis…

Mystery solved!

It seems that if the face starts at an ‘end’ point of an edge that is flipped (edges have a ‘start’ and ‘end’ position) it doesn’t work. Not sure if I described it right but this gif should demonstrate.

EDIT:

Further testing reveals that this only occurs if the edges is exactly the same length, and position as the first edge created when making the face, but, the edge has the start and end positions reversed.

@Neil_Burkholder is this then… a bug? :smiley:

IMHO Yes

@Neil_Burkholder if I flip the faces, will this get rid of the problem? Also is there any way of flipping faces from the Ruby API?

If you mean exchange the front and back, yes: Face#reverse!

No, but changing your starting point will, as shown in one of my previous posts.

You could check for and split any edges that are reverse oriented from your starting point.

This works on your original model.

ent = SketchUp.active_model.entities
pts = [
[5.65553, 11.15667, 0.0], [5.65553, 6.15667, 0.0], [5.65553, 6.15667, 2.7], 
[5.65553, 11.15667, 2.7], [5.65553, 11.15667, 0.0], [5.65553, 10.33172, 0.87517], 
[5.65553, 10.33172, 2.09517], [5.65553, 7.18172, 2.09517], [5.65553, 7.18172, 0.87517], 
[5.65553, 10.33172, 0.87517]]

e = ent.grep(Sketchup::Edge).find_all {|e| e.end.position==pts[0] && e.start.position==pts[1]}
e.each{|e| e.split(0.5)}

ent.add_face(pts)

Or if you always want to prevent this you could create a refinement. Thanks to @DanRathbun for showing me how to fix things with refinements.

module Author

  module Author::RefinedComponents
	  
    refine ::Sketchup::Entities do
		
      # fixes a bug that prevents faces from being created, when the first edge already exists in a reverse orientation.
      #split the offending edge to allow the new face to be created always.
	  
      def add_face_force(pts)
        e = parent.entities.grep(Sketchup::Edge).find_all {|e| e.end.position==pts[0] && e.start.position==pts[1]}
	    e.each{|e| e.split(0.5)}
        parent.entities.add_face(pts)
	  end
	  
    end # class refinement
	
  end # refinement module
  
end #Author Module

using Author::RefinedComponents

module Author::SomePlugin

  # Use your refinements here.
  pts = [
  [5.65553, 11.15667, 0.0], [5.65553, 6.15667, 0.0], [5.65553, 6.15667, 2.7], 
  [5.65553, 11.15667, 2.7], [5.65553, 11.15667, 0.0], [5.65553, 10.33172, 0.87517], 
  [5.65553, 10.33172, 2.09517], [5.65553, 7.18172, 2.09517], [5.65553, 7.18172, 0.87517], 
  [5.65553, 10.33172, 0.87517]]

  Sketchup.active_model.entities.add_face_force(pts)
 
 
end # author's plugin module


@Neil_Burkholder, @DanRathbun thank you both, will test and update if necessary… hopefully they will fix this in an upcoming patch if it’s indeed a bug.

@Neil_Burkholder it seems that the above code breaks the object (leaves it with a hole in the mesh), as it now has no volume. Any fixes for that?

Can you post an example? Dividing an edge shouldn’t delete any faces. When the new face is created, the split edge is overwritten as one continuous edge. Did you try running the above code on your original model? It worked when I tried it.

@Neil_Burkholder that’s easy to show without posting anything use that scene and when you add the face just make all of the geometry a group. You’ll see that it won’t have any volume, if you use Solid you’ll see that the mesh is incomplete.

Hmm I failed to notice that. The split edge is in fact not overwritten. It’s still there, co-existing with the new continuous edge.

You could try something like this.

ent = SketchUp.active_model.entities
pts = [
[5.65553, 11.15667, 0.0], [5.65553, 6.15667, 0.0], [5.65553, 6.15667, 2.7], 
[5.65553, 11.15667, 2.7], [5.65553, 11.15667, 0.0], [5.65553, 10.33172, 0.87517], 
[5.65553, 10.33172, 2.09517], [5.65553, 7.18172, 2.09517], [5.65553, 7.18172, 0.87517], 
[5.65553, 10.33172, 0.87517]]

e = ent.grep(Sketchup::Edge).find_all {|e| e.end.position==pts[0] && e.start.position==pts[1]}
e.each{|e| e.erase!}

face = ent.add_face(pts)

face.edges.each {|e| e.find_faces}