LoginSignup
1
2

More than 5 years have passed since last update.

SketchUp Make v17 / Ruby > 複数のGroupを整列する > X方向

Last updated at Posted at 2016-12-30
動作環境
SketchUp Make v17.1.173
MacOS X El Capitan

関連 http://qiita.com/7of9/items/ff5921cf65b5f88d9311

NIC directでフレーム発注する予定のlaundry rackのデザイン。 

フレームの幅を変更した時の位置合わせが面倒だ。

API/Rubyの勉強ついでに実装してみた。

code

align161230.rb
require 'sketchup.rb'

def align_group_to_worldOrigin()
    model = Sketchup.active_model
    my_selection = model.selection

    my_selection.each do |ent|
        if ent.is_a? Sketchup::Group
        tr = ent.transformation
        org = tr.origin # :Point3d
        pnt = Geom::Point3d.new(-org[0], -org[1], -org[2])
        mve = Geom::Transformation.new(pnt)
        ent.transform! mve
        end
    end
end

def align_groups_center_xdirection()
    model = Sketchup.active_model
    my_selection = model.selection

    firstGroup = true
    center0 = 0.0 # set 0.0 for placeholder of double type (not good coding)
    my_selection.each do |ent|
        if !(ent.is_a? Sketchup::Group) then
             continue
        end

        # calculate x center of group[0]
        tr = ent.transformation
        org = tr.origin # :Point3d
        cnt = org[0] + ent.bounds.width / 2.0 # center in x direction

        if firstGroup then
            # store x center of group[0]
            firstGroup = false
            center0 = cnt
        else
            # move groups according to saved info
            xshift = center0 - org[0] - ent.bounds.width / 2.0
            pnt = Geom::Point3d.new(xshift, 0, 0)
            mve = Geom::Transformation.new(pnt)
            ent.transform! mve
        end
    end
end

align_groups_center_xdirection()を使う。

実行手順

  1. 複数のGroupを選択する
  2. Rubyコンソールでalign_groups_center_xdirection()を実行する

整列前

qiita.png

整列後 

qiita.png

座標計算について

  • center0: group[0]の中心座標(X方向)

group[1..]の移動については以下としている。

  1. orgとcenter0との差分を計算して、一旦group[0]の0に合わせる
  2. group[1..]のwidth/2分だけ補正しなおすことで、group[1..]の中心に合わせる

上の1,2を合わせたのが以下。
xshift = center0 - org[0] - ent.bounds.width / 2.0

試用1

左の構造物のX軸をあわせることができた。

qiita.png

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2