Create a polygon mesh and colour each polygon

mesh

#1

I have a text file with coordinates and rgb values. I read the file and insert faces like this.

if File.exists?(filepath)
filename = File.basename(filepath,".txt")
strarr = File.readlines(filepath)
addLayer(filename,true)

strarr.each{|string|
pts = Array.new
begin
pts[0] = [string.split(',')[0].to_f,string.split(',')[1].to_f,string.split(',')[2].to_f]
pts[1] = [string.split(',')[3].to_f,string.split(',')[4].to_f,string.split(',')[5].to_f]
pts[2] = [string.split(',')[6].to_f,string.split(',')[7].to_f,string.split(',')[8].to_f]

face = entities.add_face(pts)	 
    edges = face.edges	
edges.each{|e|
e.layer = filename
e.hidden=true
}
face.material = [string.split(',')[9].to_i,string.split(',')[10].to_i,string.split(',')[11].to_i]
face.layer = filename

rescue 
next
end	
}	
end		

But it is taking long time for more number of faces. I usually insert more than 20000 faces. Now i am trying to use polygon mesh instead of faces(as it is quicker). I want to colour each polygon of the mesh and also hide the edges.
Is there any sample code for creating a polygon mesh and colouring each polygon with a colour?

Thanks.


#2

I would try to optimise the code first to avoid stack overflow…

and to keep all entities on Layer0

I can’t test without an example file, but you can…

if File.exists?(filepath)
	filename = File.basename(filepath,".txt")
	strarr = File.readlines(filepath)
	
  # add a group instead	
  new_grp = entities.add_group()
	
  new_grp.name  = filename
  new_grp.layer = filename
  
  gents = new_grp.entities
  
strarr.each do |string|

    items = string.split(',')

    # cheers @DanRathbun  
    pts = [
      Geom::Point3d::new(items[0..2]),
      Geom::Point3d::new(items[3..5]),
      Geom::Point3d::new(items[6..8])
    ]

    face = gents.add_face(pts)
		 
    face.material = items[9..11].each{|e| e.to_i}
	
   rescue 
      next
   end # strarr.each

   new_grp.grep(Sketchup::Edge){|e| e.hidden = true }

end	

john


#3

I heard that polygon mesh is much faster. How can i create polygon mesh with the above vertices ?


#4

Geom::PolygonMesh will be no quicker if the performance issue is how you extract the information…

did you even try optimising your code?

once you have clean input you can test the speed of each method for yourself…

supply a sample txt file if you want more constructive help…

john


#5

PolygonMesh will be quicker to load from your file because it is only “helper geometry”, that is, a standalone data object that does not create any geometry in the SketchUp model. You have to invoke Entities#add_faces_from_mesh or Entities#fill_from_mesh to create Faces and Edges corresponding to the polygons in the PolygonMesh. Because they are bulk operations running in compiled C behind the Ruby API instead of a Ruby loop in your code, those methods should be significantly faster than your code. But this may not be suitable for what you describe because these methods only provide ways to apply one kind of smoothing/softening to all Edges and one front material and back material for all the Faces. You can not color them one by one based on your file data without an expensive step to find which Face corresponds to each polygon from your data.


#6

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.