C#ATIA

↑タイトル詐欺 主にFusion360API 偶にCATIA V5 VBA(絶賛ネタ切れ中)

迷路を作る2

こちらの続きです。
迷路を作る - C#ATIA

先日のスピードテストを反映させて、処理時間の短縮させたつもりなのですが
まだまだ遅いです。

念の為、コードを公開。
迷路自体は、こちらのライブラリを利用しています。
GitHub - boppreh/maze: Simple maze generator in Python
'maze.py' だけを利用しているので、スプリクトと同一フォルダ内に
置いて下さい。

#FusionAPI_python test_Maze ver0.0.2
#Author-kantoku
#Description-迷路ボディの作成

'''
The MIT License (MIT)

Copyright (c) 2014 BoppreH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''

import adsk.core, adsk.fusion, traceback
import os, sys, time
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

#thx-boppreh
#https://github.com/boppreh/maze
import maze

def run(context):
    row_num = 10
    column_num = 10
    
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        NewDoc(app)
        root = app.activeProduct.rootComponent
        
        #拡張
        maze.Maze.exp = expWallMap
        
        #time
        t = time.time()
        
        #迷路取得
        mz = maze.Maze.generate(row_num,column_num)
        wall = mz.exp()
        print('迷路取得:{:.2f}s'.format(time.time()- t))
        
        #スケッチ作成
        unit = 2.0
        
        skt = root.sketches.add(root.xYConstructionPlane)
        
        QuickOn(skt)
        for j,y in enumerate(wall):
            for i,x in enumerate(y):
                if x:
                    initBox(skt,(i*unit,j*unit,0),((i+1)*unit,(j+1)*unit,0))
        QuickOff(skt)
        print('スケッチ:{:.2f}s'.format(time.time()- t))
        
        #パッド
        joinBody =  adsk.fusion.FeatureOperations.JoinFeatureOperation
        height = adsk.core.ValueInput.createByReal(unit)
        exts = root.features.extrudeFeatures
        
        objs = lst2objs([prof for prof in skt.profiles if IsQuad(prof)])
        exts.addSimple(objs, height, joinBody)
        print('パッド:{:.2f}s'.format(time.time()- t))
        
        #finish
        ui.messageBox('done\ntime:{:.2f}s'.format(time.time()- t))

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
            
def NewDoc(app):
    return app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
    
def IsQuad(prof):
    profCurves = prof.profileLoops.item(0).profileCurves
    return True if profCurves.count == 4 else False

def QuickOn(skt):
    skt.isComputeDeferred = True
    skt.areProfilesShown = False
    skt.areDimensionsShown = False

def QuickOff(skt):    
    skt.areDimensionsShown = True
    skt.areProfilesShown = True
    skt.isComputeDeferred = False  

def initBox(skt,ary1,ary2):
    lines = skt.sketchCurves.sketchLines
    box = lines.addTwoPointRectangle(
        adsk.core.Point3D.create(ary1[0],ary1[1],ary1[2]),
        adsk.core.Point3D.create(ary2[0],ary2[1],ary2[2]))
    return box
    
def lst2objs(lst):
    objs = adsk.core.ObjectCollection.create()
    [objs.add(obj) for obj in lst]    
    return objs
    
#maze.Maze
def expWallMap(self):
    return [convIsWallLst(v) for v in self._to_str_matrix()[::-1]]

def convIsWallLst(lst):
    return [True if v == 'O' else False for v in lst]

面倒なため、こちらにまとめてデータをUpしました。
3D CAD Model Collection | GrabCAD Community Library

設定を変えれば、こんな感じのものが出来ます。
f:id:kandennti:20181001114648p:plain
でも、5分ぐらい時間がかかります。

処理時間を調べると
迷路取得:0.01s
スケッチ:254.86s
パッド:280.70s
こんな感じで、スケッチの処理時間が予想外に多かったです。
1マス毎にスケッチを描いているのが無駄なのは、気が付いて
いるのですが、どの様にすれば効率良くなるのだろう?
(グラフ構造っぽいことをしなきゃならないのかな・・・)