g = Sketchup.active_model.selection[0] gps = g.entities.grep(Sketchup::Group) gp1 = gps.find{|gp| gp.get_attribute("DFC_AN_ZsggTool","构件群组") == "竖骨线组"} path_hash = { s_sk_path: File.expand_path("../曲面竖向龙骨.skp", __FILE__) } hash.merge!(path_hash) class A # 初始化方法 def initialize(g,gp1,hash) @g = g @gp1 = gp1 @hash = hash end # 竖向龙骨方法 def skeel definitions = Sketchup.active_model.definitions d = definitions.load @hash[:s_sk_path] d_depth = d.bounds.depth if @hash["墙类型"] == "平形" mypt = @mg.entities.add_cpoint ORIGIN @gp1.entities.each{|e| need_df_name = get_SLG_df_name(e) need_df = definitions.find{|df| df.name==need_df_name} pt = e.vertices.sort_by{|v| v.position.z}.map(&:position).first pt.offset!(@f_normal.reverse,d_depth/2) if need_df.nil? need_df = get_SLG_defintion(e, d, definitions) end @mg.entities.add_instance need_df,pt } @mg.entities.erase_entities mypt elsif @hash["墙类型"] == "弧形" mypt = @mg.entities.add_cpoint ORIGIN @gp1.entities.each{|e| need_df_name = get_SLG_df_name(e) need_df = definitions.find{|df| df.name==need_df_name} pt,pt_ = e.vertices.sort_by{|v| v.position.z}.map(&:position) vec = e.get_attribute("线","方向") pt.offset!(vec,d_depth/2) if need_df.nil? need_df = get_SLG_defintion(e, d, definitions) end copins = @mg.entities.add_instance need_df,pt fz_vec = X_AXIS.reverse.transform copins.transformation #凹口向量 circle_vec = e.get_attribute("线","方向") #龙骨朝向圆心的向量 angle = fz_vec.angle_between circle_vec face = d.entities.grep(Sketchup::Face).first c_p = face.bounds.center.transform copins.transformation t1 = Geom::Transformation.rotation c_p, Z_AXIS, angle t1 = Geom::Transformation.rotation c_p, Z_AXIS.reverse, angle if not (circle_vec.samedirection? fz_vec.transform t1) copins.transform! t1 } @mg.entities.erase_entities mypt elsif @hash["墙类型"] == "倒弧形" edges = @gp1.entities.select{|ent| ent.typename == "Edge"} #竖线组竖线 slg_edges = get_Sorted_edges(edges) slg_edges.each{|e| need_df_name = get_SLG_df_name(e) need_df = definitions.find{|df| df.name==need_df_name} pts = get_pts e bp = pts.sort_by{|p| p.z}.first if need_df.nil? need_df = get_SLG_defintion(e, d, definitions) end @mg.entities.add_instance need_df, bp } end end #获取竖龙骨的定义的名字 def get_SLG_df_name(e) if @hash["墙类型"] == "平形"||@hash["墙类型"] == "弧形" sum_l = e.length sum_l_s = sum_l.to_mm.to_i.to_s return sum_l_s+"_para_SLG" elsif @hash["墙类型"] == "倒弧形" sum_l = e.map(&:length).inject(:+) sum_l_s = sum_l.to_mm.to_i.to_s if e.length > 1 ed0 = e.first v0, v1 = ed0.vertices ed1 = e.find{|ed| ed.vertices.include?(v0) && ed!=ed0} if ed1 common_v = v0 else ed1 = e.find{|ed| ed.vertices.include?(v1) && ed!=ed0} common_v = v1 end v0,v1 = [ed0,ed1].map{|ed| ed.vertices.find{|v| v != common_v}} common_p, v_p0, v_p1 = [common_v, v0, v1].map(&:position) angle = (common_p.vector_to v_p0).angle_between (common_p.vector_to v_p1) angle_s = angle.round(2).to_s else angle_s = "0" end return sum_l_s+"_"+angle_s+"_arc_SLG12" end end #获取竖龙骨新的定义 def get_SLG_defintion(e, d, definitions) need_df = definitions.add(get_SLG_df_name(e)) if @hash["墙类型"] == "平形"||@hash["墙类型"] == "弧形" need_df.description = "平行墙竖向龙骨" copins = need_df.entities.add_instance(d,ORIGIN) pt, pt_ = e.vertices.sort_by{|v| v.position.z}.map(&:position) #获取底部的点 vec = pt.vector_to pt_ face = d.entities.grep(Sketchup::Face).first f_normal = face.normal.transform copins.transformation c_p = face.bounds.center.transform copins.transformation angle = f_normal.angle_between vec t0 = Geom::Transformation.rotation c_p, Y_AXIS, angle t0 = Geom::Transformation.rotation c_p,Y_AXIS.reverse, angle if not (vec.samedirection? f_normal.transform t0) copins.transform! t0 fz_vec = Z_AXIS.transform copins.transformation #凹口向量 angle = fz_vec.angle_between @f_normal #墙面向量 t1 = Geom::Transformation.rotation c_p, Z_AXIS, angle t1 = Geom::Transformation.rotation c_p, Z_AXIS.reverse, angle if not (@f_normal.samedirection? fz_vec.transform t0) copins.transform! t1 if @hash["墙类型"] == "平形" face = copins.explode.find{|e|e.typename=="Face"} face.pushpull e.length need_df.set_attribute("方向",e.get_attribute("线","方向")) return need_df elsif @hash["墙类型"] == "倒弧形" pts = get_pts e bp = pts.sort_by{|p| p.z}.first copins = need_df.entities.add_instance d,ORIGIN #放进小组中 bp_= pts.sort_by{|p| p.z}[1] bp_ed = e.find{|ed| ps = ed.vertices.map(&:position); ps.include?(bp) && ps.include?(bp_)} vec_0 = bp.vector_to bp_ face = d.entities.grep(Sketchup::Face).first f_normal = face.normal.transform copins.transformation c_p = face.bounds.center.transform copins.transformation angle = f_normal.angle_between vec_0 z_vec = vertical_vec_for_2vec(f_normal, vec_0) #两向量的法向量 trs = Geom::Transformation.translation [0,0,-d.bounds.depth*0.5] trs0 = (Geom::Transformation.rotation c_p, z_vec, angle) trs0 = (Geom::Transformation.rotation c_p, z_vec.reverse, angle) if not (f_normal.transform trs0).samedirection? vec_0 copins.transform! trs0*trs fz_vec = Z_AXIS.reverse.transform copins.transformation #凹口向量 vec_1 = bp_ed.get_attribute "线","方向" #龙骨朝向圆心的向量 angle = (fz_vec.angle_between vec_1) trs1 = Geom::Transformation.rotation c_p, vec_0, angle trs1 = (Geom::Transformation.rotation c_p, vec_0.reverse, angle) if not (fz_vec.transform trs1).samedirection? vec_1 copins.transform! trs1 face = copins.explode.find{|e|e.typename=="Face"} face.reverse! if face.normal.samedirection? vec_0 path = need_df.entities.add_edges pts.map{|pt| pt.transform (bp.vector_to ORIGIN)} face.followme path need_df.entities.erase_entities path need_df.entities.grep(Sketchup::Edge).select{|ed| ed.soft = true if ed.length == @hash["竖骨宽"]} need_df.set_attribute "竖骨", "方向", vec_1 return need_df end end #获得一组一组的连接的线 def get_Sorted_edges(edges) bord_eds = edges.select{|ed| (edges-[ed]).select{ |ed_| common_pt(ed, ed_) }.length == 1} #多条边相连 x_eds_arr = [] #存放各个temp_arr @use_eds = [] #放置已经使用过的edge bord_eds.each do |ed| #求与ed相连的一串edges, 放于temp_arr next if @use_eds.include?(ed) @use_eds.push ed temp_arr = [ed] start_ed = ed while 1 do con_edge = ([email protected]_eds).find{|ed_| common_pt(start_ed, ed_)} if con_edge.nil? break else @use_eds.push con_edge temp_arr.push con_edge start_ed = con_edge end end x_eds_arr.push temp_arr #x_eds_arr中存放一个个的竖线组 end return x_eds_arr end #获取两个向量之间的法向量 def vertical_vec_for_2vec(vec1, vec2) raise "Error: 两向量平行" if vec1.parallel? vec2 x1,y1,z1 = vec1.to_a x2,y2,z2 = vec2.to_a x, y, z = (y1*z2-y2*z1),(z1*x2-z2*x1),(x1*y2-x2*y1) return Geom::Vector3d.new(x, y, z) end def common_pt(ed1,ed2) ed1.vertices.map(&:position).find{|pt| ed2.vertices.map(&:position).include?(pt)} end def get_pts(edges) pts = [] pts << edges.first.vertices.reject{|pt| true if pt.position == common_pt(edges.first,edges[1])}.first.position edges.each{|e| e.vertices.each{|v| pts << v.position if (!pts.include?(v.position))}} pts end def combination group_SLG = skeel end end Sketchup.active_model.start_operation "" A.new(g,gp1,gp2,gp_mesh, gp1_face, hash).combination Sketchup.active_model.commit_operation
原文地址:https://www.cnblogs.com/zhongshengdong/p/8930906.html
时间: 2024-09-30 20:40:02