How do I create a face from edge objects?

advice
question

#1

Good Morning my Friends,

As I am new to SketchUp Ruby and and used an old manual to learn the basics, there are still a few things that I cant do or understand how to do.

The problem I´m currently facing is generating a face from several edge objects.
E.g. if I create an arc with the add_arc method I can´t add a face, as It isnt a “closed” edge object. If I then add a curve which links the two ends of the arc I have two edge objects that form a ring together. Still, they arent really forming a closed edge object, it just looks like that, and thats why I can´t add a face to it.
Is there any way I can combine these two into a single edge object or another method which would allow me to add a face on the “arc-curve-object”?

Thanks for your help in advance!


#2

The methods like entities.add_edges(), entities.add_arc(), entities.add_circle() etc create and return an array of edge objects in the given entities context.
You can then use those in several ways.
e.g. face = entities.add_face(edges) - it can take a list or an array of points, vertices, edges as its argument - even a curve object works if it loops, like a circle… the method returns a reference to the face, if successful.
or count = edges[0].find_faces could be used for the first edge in the array… it returns the number of faces found, to get the face[s] use: faces = edges[0].faces, there might be more that one face found depending on the preexisting edges pattern - face = faces[0] if there’s only one…

It depends on what you want to do…


#3

Thanks for your fast reply.
That means that I can add two or more edges into the add_face method?
I tried that but it doesnt work the way I did it:

ents=Sketchup.active_model.entities

pt1 = [1.m,0,0]
pt2 = [0,0,0]
pt3 = [0,0,1.m]
nv = [0,1.m,0]
r = 1.m
sa = 0.degrees
ea = 90.degrees

edge1 = ents.add_curve pt1 ,pt2, pt3
arc = ents.add_arc pt2, pt3, nv, r, sa, ea
face1 = ents.add_face (edge1, arc)

The ruby console says that there shouldnt be a comma after edge1 in the add_face method. I also tried to safe edge1 and arc into an array and putting the array into the add_face method, but that also didnt work.

This is what I currently have…

grafik


#4

…and this is what i want to achieve.
grafik


#5

Try using the other option that TIG mentioned. The find_faces method.

So something like this:

ents=Sketchup.active_model.entities

pt1	= [1.m,0,0]
pt2	= [0,0,0]
pt3	= [0,0,1.m]
nv = [0,1.m,0]
r	= 1.m
sa	= 0.degrees
ea	= 90.degrees

edge1 = ents.add_curve pt1 ,pt2, pt3
arc = ents.add_arc pt2, pt3, nv, r, sa, ea
edge1[0].find_faces

#6

Try this…

model = Sketchup.active_model
model.start_operation('add_face', true) # your method's name

pt1 = [1.m, 0, 0]
pt2 = [0, 0, 0]
pt3 = [0, 0, 1.m]

nv = [0, 1.m, 0] # or just use Y_AXIS ?
ra = 1.m
sa = 0.degrees
ea = 90.degrees

ents = model.active_entities

puts
p edges2 = ents.add_edges( pt1, pt2, pt3 ) # array of 2 new edges NOT 'curve'
puts
p arc_edges = ents.add_arc( pt2, pt3, nv, ra, sa, ea ) # array of new arc's edges
puts
p all_edges = edges2 + arc_edges # new array of both
puts
p face = ents.add_face( all_edges ) # face object created or 'nil'
puts

model.commit_operation # inside 1 undo step

I added the p & puts to show you what’s happening in the Ruby Console…
Please use () around passed arguments…


#7

The add_face method isn’t expecting to have one array of edges followed by another array of edges. You’ll need to concatenate the two arrays and supply it as a single argument.

That’d look like this:

ents=Sketchup.active_model.entities

pt1	= [1.m,0,0]
pt2	= [0,0,0]
pt3	= [0,0,1.m]
nv = [0,1.m,0]
r	= 1.m
sa	= 0.degrees
ea	= 90.degrees

curve = ents.add_curve pt1 ,pt2, pt3
arc = ents.add_arc pt2, pt3, nv, r, sa, ea

ents.add_face(curve + arc)

EDIT: Oops it appears I am only reiterating some of what was already said by TIG.


#8

Thanks a lot for your help! The booklet i learned from said, that parantheses arent necessery, but ill keep your advice in mind.
Even though you answered my original question i still have three additional questions.
First off: What does the second line you wrote meant?
Secondly: Can you recommend a book or something similar that teaches one to programm in ruby? I reckon the one im learning with is a bit old and thereby not that fitting to the newer versions of sketchup.

Thanks a lot again for your help!


#9

Current versions of Ruby don’t allow a space between a method name and the paren starting the argument list.


#10

The current thinking is that only global methods from module Kernel, and classes BasicObject and Object have “keyword status” and can be used without parenthesis.

All other method calls should use parenthesis around method arguments. Newer versions of Ruby can spit out warnings to the console when parenthesis are not used. These warnings have a mention that parenthesis may be required in the future. (I imagine this would happen, if it does, in a major release like 3.0 which I think may even have a beta available now.)