Pro/Eで木構造

Pro/Engineeringにはアセンブリの構成部品一覧をhtmlで出力してくれる機能がついている.

でも,これが個人的にちょっと見難い形になっている.
アセンブリleaf nodeを部品とする木構造を持っている.こんな感じ.

WHOLE.asm
|
|-- A.asm
|   |-- a.prt
|   |-- D.asm
|      |-- b.prt   
|-- B.asm
|   |-- c.prt
|-- C.asm
    |-- d.prt

でも,Pro/Eが生成してくれるのは

WHOLE.asm
|-- A.asm  
|-- B.asm
|-- C.asm

A.asm
|-- a.prt
|-- D.asm

D.asm
|-- b.prt 

こういう形のhtml.アセンブリの見通しがちょっと悪い.それに生データを色々いじりたいのに対して,ソースが

<script>
        appendCellsToTableRow(
  '<A href="proep://local_proesession?oid=WHOLE_FM.ASM&item=FEATURE&id=67&action=highlight"
  alt="グラフィックウィンドウでオブジェクトをハイライト" title="グラフィックウィンドウでオブ
  ジェクトをハイライト" >PANEL_MY</A>')
</script><script>
    appendCellsToTableRow (
  '<div ptc="ptc_no_spacing"></div><table width="100%" border="0" cellpadding="0" cellspacing
  ="0"><tr><td width="42" valign="top"><div align="center"><a  href="proep://local_proesessi
  on?oid=WHOLE_FM.ASM&&item=feature&id=67&action=highlight">
  <img src="file:///C:/Program Files/proeWildfire 4.0/proe/prowttools/text/images/downloadc
  ad.gif" alt="グラフィックウィンドウでオブジェクトをハイライト" title="グラフィックウィン
  ドウでオブジェクトをハイライト" border="0" align="absmiddle"/></a></div></td><td width="5
  0" valign="top"><div align="center"><a href="...

みたいなjs地獄になってるので,これはこのまま扱いようが無い.というわけでやや久々にPerl.
何はともあれhtmlをパースして@$tableにぶちこみ,

# Tree構造の生成
foreach my $content (@content) {
    if($content =~ /"TABLE_BOM_REPORT_BREAKDOWN_(.+)"/){
        $p++;
        $table->[$p] = {};
        $table->[$p]->{name} = $1;
        $table->[$p]->{quantity} = 1;
        $table->[$p]->{child} = [];
        $table->[$p]->{mass} = $stiff->{$1}->{mass};
        $table->[$p]->{density} = $stiff->{$1}->{density};
        $table->[$p]->{staff} = $stiff->{$1}->{staff};
        $table->[$p]->{color} = $stiff->{$1}->{color};
        $table->[$p]->{type} = $stiff->{$1}->{type};
        color($table->[$p]);
    }
    if($content =~ /appendCellsToTableRow \('([0-9]+)'\)/){
        $hash->{quantity} = $1;
    }
    if($content =~ />(.+)<\/A>'\)/){
        $hash->{name} = $1;
        $hash->{child} = [];
        $hash->{mass} = $stiff->{$1}->{mass};
        $hash->{density} = $stiff->{$1}->{density};
        $hash->{staff} = $stiff->{$1}->{staff};
        $hash->{color} = $stiff->{$1}->{color};
        $hash->{type} = $stiff->{$1}->{type};
        color($hash);
        push @{$table->[$p]->{child}}, $hash;
        undef $hash;
    }
    last if $content =~ /"TABLE_BOM_REPORT_SUMMERY/;
}

深さ優先で$tableの各枝を$treeに接ぎ木.

for(@$table){
    marge($tree,$_);
}

sub marge {
    my($parent,$child) = @_;
    my $w = 0;
    while(1){
        unless($parent->{"child"}->[$w]){
            dequeue($child);
            last;
        }
        if(@{$parent->{"child"}->[$w]->{"child"}}){
            push @$queue, @{$parent->{"child"}->[$w]->{"child"}};
        }
        if($child->{"name"} eq $parent->{"child"}->[$w]->{"name"}){
            undef $parent->{"child"}->[$w];
            $parent->{"child"}->[$w] = $child;
        }
        $w++;
    }
}

sub dequeue {
    my $child = shift;
    my $node = shift @$queue;
    push @$queue, @{$node->{"child"}} if $node->{"child"};
    $node->{"child"} = $child->{child} if $node->{"name"} eq $child->{"name"} && $node->{"name"};
    dequeue($child) if $queue->[0];
}

変数名がTekitou!ともかく質量,密度,材料,種別などの情報(こちらは別スクリプトで吐かせたテキストを読み込んでいる)を含んだ木構造ができた.ためしにWin32::OLEでExcelにダンプしてみる.


これで材料設定がおかしいパーツを特定したり,質量分布をグラフ化したりも楽ちん.俺得.