Use area to get length from groups,don't use the edge

mod = Sketchup.active_model # 打开模型
ent = mod.entities # 模型中的所有实体
sel = mod.selection # 当前选择
def min(*args)
   args.min
end

def max(*args)
   args.max
end
dn=1
sel.each do | e |
 if e.is_a? Sketchup::Group
 e.name=dn.to_s
 dn=dn+1
 end
end

chosen_image = UI.savepanel("保存", mod.path, "csv|*.csv|txt|*.txt||")
puts chosen_image
 p=mod.path.gsub(/.skp$/,'_TMY.csv')
#f=File.open(p,'w')
sel.each do | e |
 if e.is_a? Sketchup::Group
    #puts e.length
    grp=e
    
    pt=grp.bounds.max
    pt1=grp.bounds.min
    w=grp.bounds.width
    h=grp.bounds.height
    d=grp.bounds.depth
    temp0=max(w,h,d)
    #最大数 temp0
    temp2=min(w,h,d)
    #最小数
    if w<temp0 then
      if w> temp2 then
      temp1=w
      end
    end
   if h<temp0 then
      if h> temp2 then
      temp1=h
      end
    end
    if d<temp0 then
      if d> temp2 then
      temp1=d
      end
    end  
    #temp1 中间数
     tn1=0
     tn2=0
    grp.entities.each do | e1 |
        if e1.is_a? Sketchup::Edge
           if e1.length == temp2 then
             if h> w
              x1, y1, z1 = e1.start.position.to_a
              #纵向
	          x2, y2, z2 = grp.bounds.corner(0).to_a
              if x1==x2 and y1 == y2 and z1 != z2
              Tn1=z1-z2
              UI.messagebox Tn1.to_mm
              end
	          
              x2, y2, z2 = grp.bounds.corner(6).to_a
              if x1==x2 and y1 == y2  and z1 != z2
              Tn2=z2-z1
              UI.messagebox Tn2.to_mm
              #puts Tn2.to_mm  
              end
              
             
             end
             if w > h then
              #水平
              x1, y1, z1 = e1.start.position.to_a
              x2, y2, z2 = grp.bounds.corner(4).to_a
              #puts x1
              #puts x2
              if y1==y2 and z1==z2 and x1!=x2
              #puts z1
              #puts z2
              
              Tn1=x1-x2
              UI.messagebox Tn1.to_mm
              end
	          
              x2, y2, z2 = grp.bounds.corner(7).to_a
              if y1==y2 and z1==z2 and x1!=x2
              
              Tn2=x2-x1
              UI.messagebox Tn2.to_mm
              #puts Tn2.to_mm  
             end
              
             end
              #puts Tn2.to_mm  
             
            end
         end
      end
      
      # ste=grp.name+",X:"+(pt[0]-pt1[0]).to_mm.round(2).to_s+"mm,"+"Y:"+(pt[1]-pt1[1]).to_mm.round(2).to_s+"mm," +"Z:"+(pt[2]-pt1[2]).to_mm.round(2).to_s+"mm"+",N1="+Tn1.to_s+",N2="+Tn2.to_s
      # f.puts ste
  end
end

will get the data of copper busbar bending
just get group (not move ) can
Tn1 and Tn2
if move the group ,the edges start point and end point no changes.
so …

I’m not entirely sure if I understand the question, but let me try;

1 You have a group with some edges.
2. You check the position of the edges
3. You move the group
4. You expect the position of the edges to update?

Entities in groups and components have coordinates local to that group/component. So if you move the group the local coordinates of the entities inside doesn’t change.

You would have to apply the transformation of the group to the local coordinates in order to get the global coordinates.

# Select a group with some edges:
model = Sketchup.active_model
group = model.selection.grep(Sketchup::Group).first
edge = group.entities.grep(Sketchup::Edge).first

puts "Before move:"

puts "Local coordinates:"
p edge.vertices.map(&:position)

puts "Global coordinates:"
p edge.vertices.map(&:position).map { |pt| pt.transform(group.transformation) }

tr = Geom::Transformation.new(Geom::Vector3d.new(10, 0, 0))
group.transform!(tr)

puts "After move:"

puts "Local coordinates:"
p edge.vertices.map(&:position)

puts "Global coordinates:"
p edge.vertices.map(&:position).map { |pt| pt.transform(group.transformation) }

nil

Output:

Before move:

Local coordinates:
[Point3d(0, 79.3307, 0), Point3d(0, 0, 0)]

Global coordinates:
[Point3d(7.25321, 167.974, 0), Point3d(7.25321, 88.6432, 0)]

After move:

Local coordinates:
[Point3d(0, 79.3307, 0), Point3d(0, 0, 0)]

Global coordinates:
[Point3d(17.2532, 167.974, 0), Point3d(17.2532, 88.6432, 0)]
2 Likes

Some ideas:


sel.each do | e |
  if e.is_a? Sketchup::Group
    grp = e
    # ...

Can be better done as:

sel.grep(Sketchup::Group) do | grp |
  # ...

(You save an indentation.)


    if w<temp0 then
      if w> temp2 then
        temp1=w
      end
    end

Can be done as:

    temp1 = w if w < temp0 && w > temp

    grp.entities.each do | e1 |
      if e1.is_a? Sketchup::Edge
        if e1.length == temp2 then
          # ...

… can be better done as:

    grp.entities.grep(Sketchup::Edge) do |e1|
      next unless e1.length == temp2
      # ...

(You save 2 indents.)

1 Like

I am will use REgroup .think it .
TKS DanRathbun and tt_su
This demo will get copper busbar bending data and qickly to get cut length bend length
will for Electric Engineering Corporation use

test group explode,and the edge.vertices no change.
image
this is test code
Sketchup.active_model.entities.add_line grp.bounds.corner(6),e1.start

# 这段是默认代码,您可以选择使用或删除。。。
mod = Sketchup.active_model # 打开模型
ent = mod.entities # 模型中的所有实体
sel = mod.selection # 当前选择
def min(*args)
   args.min
end

def max(*args)
   args.max
end
dn=1
sel.grep(Sketchup::Group) do | e |
 
 e.name=dn.to_s
 dn=dn+1
 
end

chosen_image = UI.savepanel("保存", mod.path, "csv|*.csv|txt|*.txt||")
puts chosen_image
 #p=mod.path.gsub(/.skp$/,'_TMY.csv')
#f=File.open(p,'w')
sel.grep(Sketchup::Group) do | grp |
    
    pt=grp.bounds.max
    pt1=grp.bounds.min
    w=grp.bounds.width
    h=grp.bounds.height
    d=grp.bounds.depth
    temp0=max(w,h,d)
    #最大数 temp0
    temp2=min(w,h,d)
    #最小数
    temp1 = w if w < temp0 && w > temp2
    temp1 = h if h < temp0 && h > temp2
    temp1 = d if d < temp0 && d > temp2
   
    #temp1 中间数
     tn1=0
     tn2=0
    grp.entities.grep(Sketchup::Edge) do |e1|
          next unless e1.length == temp2
             if h> w
              x1, y1, z1 = e1.start.position.to_a
              #纵向
	          x2, y2, z2 = grp.bounds.corner(0).to_a
              if y1==y2 and z1 != z2
              Tn1=z1-z2
              UI.messagebox Tn1.to_mm
              end
	          
              x2, y2, z2 = grp.bounds.corner(6).to_a
              if y1==y2 and  z1 != z2
              Tn2=z2-z1
              UI.messagebox Tn2.to_mm
              #puts Tn2.to_mm  
              end
              Sketchup.active_model.entities.add_line grp.bounds.corner(0),e1.start
             
             end
             if w > h then
              #水平
              x1, y1, z1 = e1.start.position.to_a
              x2, y2, z2 = grp.bounds.corner(4).to_a
              #puts x1
              #puts x2
              if y1==y2 and z1==z2 and x1!=x2
              #puts z1
              #puts z2
              
              Tn1=x1-x2
              UI.messagebox Tn1.to_mm
              end
	          
              x2, y2, z2 = grp.bounds.corner(7).to_a
              if y1==y2 and z1==z2 and x1!=x2
              
              Tn2=x2-x1
              UI.messagebox Tn2.to_mm
              #puts Tn2.to_mm  
             end
              Sketchup.active_model.entities.add_line grp.bounds.corner(6),e1.start
             end
              #puts Tn2.to_mm  
             
            end
         end
      
      
      # ste=grp.name+",X:"+(pt[0]-pt1[0]).to_mm.round(2).to_s+"mm,"+"Y:"+(pt[1]-pt1[1]).to_mm.round(2).to_s+"mm," +"Z:"+(pt[2]-pt1[2]).to_mm.round(2).to_s+"mm"+",N1="+Tn1.to_s+",N2="+Tn2.to_s
      # f.puts ste
 



#f.close
result = UI.messagebox '导出完成,导出内容在当前文件所在目录下', MB_OK

use face area to get length.
so 方法总比困难多。
TKS
现将代码奉上,总是有wv

# 这段是默认代码,您可以选择使用或删除。。。
mod = Sketchup.active_model # 打开模型
ent = mod.entities # 模型中的所有实体
sel = mod.selection # 当前选择
def min(*args)
   args.min
end

def max(*args)
   args.max
end

 #p=mod.path.gsub(/.skp$/,'_TMY.csv')
#f=File.open(p,'w')
sel.grep(Sketchup::Group) do | grp |
    
    pt=grp.bounds.max
    pt1=grp.bounds.min
    w=grp.bounds.width
    h=grp.bounds.height
    d=grp.bounds.depth
    temp0=max(w,h,d)
    #最大数 temp0
    temp2=min(w,h,d)
    #最小数
    temp1 = w if w < temp0 && w > temp2
    temp1 = h if h < temp0 && h > temp2
    temp1 = d if d < temp0 && d > temp2
 
    #temp1 中间数
     grp.entities.grep(Sketchup::Edge) do |e1|
           next unless e1.length == temp2 #当边长等于排宽时
             
              #result = e1.faces[0].classify_point(pt1)
              #if result == Sketchup::Face::PointNotOnPlane
                  if e1.faces[0].area > e1.faces[1].area
                    bc1=e1.faces[0].area / e1.length
                    p bc1.to_mm  
                  else
                    bc2=e1.faces[1].area / e1.length
                    p bc2.to_mm 
                  end
                #end #end result
     end #end temp2
          

end             

TKS!

更新了一下取值,以免temp0 与 temp1 一样大时无法取得.采用数组方式获取就不存在这样的问题
The value is updated to avoid that it cannot be obtained when temp0 is as large as temp1. There is no such problem when it is obtained by array

Please use 2 space indents for Ruby code.

For readability, please use space before and after operators.
Re:
s.delete_if{|e| e==s.min }
… is more readable as:
s.delete_if { |e| e == s.min }


It is not allowed to define global methods in the top level ObjectSpace in a shared Ruby process like that which SketchUp uses.

Methods like you’ve shown should be defined within your extension submodule or within a class within your extension submodule.

Further, I myself see no benefit to creating generic max(*args) or min(*args) methods like this as they are already defined upon all the classes that need them. (You’ve also not used them in your final edition.)

1 Like

THKS,DanRathbun
这两个函数确实未派上用场,可以删除掉.
在语法上还得多多向前辈学习.
确实这样更方便阅读与学习.
感谢你的回复,这样让我们在语法上有所提升.
These two functions are really useless and can be deleted
I have to learn more from my predecessors in grammar
Indeed, this is more convenient for reading and learning
Thank you for your reply, so that we can improve our grammar

1 Like

在这份代码里,如果群组经过缩放或移动,仍然存在读取不出的情况.
只要群组TEMP2方向的值不变动,勉强可以使用.其他方向都可以移动改变.
In this code, if the group is scaled or moved, it still cannot be read
As long as the value of group temp2 direction does not change, it can be used reluctantly. Other directions can be moved and changed
查看铜排参数及展开下料清单

I’m not sure I understand what the question currently is.

Which value are we trying to get from Group “temp2”?

temp2 is not a group. It is the minimum dimension out of the 3 (width, height, depth) of the BoundingBox group being iterated.

So firstly the OP is relying upon the groups being aligned with the model axes in order to “sniff” out the width of the group “strip” objects. (I don’t know what they are, but I can imagine them to be like formed sheet metal strips.)

So the code is grepping groups from the selection, in each group finding the width of the strip (via minimum dimension of bounds,) then grepping the group’s edges and iterating them which are equal to the width (temp2,) selecting the adjoining face which has the largest area (I think to avoid the tiny end face,) then dividing that face by the width to get the length, and lastly printing this value in millimeters.)

So, to me it looks as though it is an exercise in calculating the length of the “strip” in the flat before bending. It will not be exact as bend allowances need to be added depending upon material and gauge (thickness).

Well, the OP mentioned that when scaled the lengths are not correct, ie …

This is because the group definition’s entities are untransformed, and the code currently does not take into account the group instances transformation.

@xlin , to use the group’s transformation, do something like:

    t = grp.transformation
    grp.entities.grep(Sketchup::Edge) do |e1|
      width = e1.length(t)
      next unless width == temp2
      faces = e1.faces
      areas = faces.map {|face| face.area(t) }
      large = areas.max
      bc = large / width
      p bc.to_mm
    end
1 Like

TKS
这段代码主要用于铜排折弯展开计算使用.
使用群组进行变换,对于电力行业来说,将会改变当前
原在SOLIDWORKS草图参数绘制,而用SU则可以随心所欲的变换修改配合使用
DanRathbun,你的代码更加简捷明了.
This code is mainly used for copper bar bending and unfolding calculation
Using groups for transformation will change the current situation for the power industry
It was originally drawn in SolidWorks sketch parameters, but with Su, it can be transformed and modified at will
Dan Rathbun, your code is simpler and clearer

非常棒,这是最终代码,

mod = Sketchup.active_model # 打开模型
ent = mod.entities # 模型中的所有实体
sel = mod.selection # 当前选择
sel.grep(Sketchup::Group) do | grp |
    s=[]
    pt=grp.bounds.max
    pt1=grp.bounds.min
    s << grp.bounds.width
    s << grp.bounds.height
    s << grp.bounds.depth
    temp0=s.max
    #最大数 temp0
    temp2=s.min
    #最小数
    s.delete_if{|e| e==temp2 } #2021.09.30修改
    temp1=s.min
    #temp1 中间数
     t = grp.transformation
     grp.entities.grep(Sketchup::Edge) do |e1|
           width = e1.length(t)
       next unless width == temp2
            #当边长等于排宽时
             
              #result = e1.faces[0].classify_point(pt1)
              #if result == Sketchup::Face::PointNotOnPlane
                  if e1.faces[0].area > e1.faces[1].area
                    bc1=e1.faces[0].area / width
                    p bc1.to_mm  
                  else
                    bc2=e1.faces[1].area / width
                    p bc2.to_mm 
                  end
                #end #end result
     end #end temp2
         
end