
import cherrypy

# 這是 MAN 類別的定義
'''
# 在 application 中導入子模組
import programs.cdag30.man as cdag30_man
# 加入 cdag30 模組下的 man.py 且以子模組 man 對應其 MAN() 類別
root.cdag30.man = cdag30_man.MAN()

# 完成設定後, 可以利用
/cdag30/man/assembly
# 呼叫 man.py 中 MAN 類別的 assembly 方法
'''
class MAN(object):
    # 各組利用 index 引導隨後的程式執行
    @cherrypy.expose
    def index(self, *args, **kwargs):
        outstring = '''
    這是 2014CDA 協同專案下的 cdag30 模組下的 MAN 類別.<br /><br />
    <!-- 這裡採用相對連結, 而非網址的絕對連結 (這一段為 html 註解) -->
    <a href="assembly">執行  MAN 類別中的 assembly 方法</a><br /><br />

       
    請確定下列零件於 V:/home/lego/man 目錄中, 且開啟空白 Creo 組立檔案.<br />
    <a href="/static/lego_man.7z">lego_man.7z</a>(滑鼠右鍵存成 .7z 檔案)<br />

    <font size="5" face="標楷體" color="#B87800">小組樂高自動組立</font><br />

    <a href="body">身體組立(40223106負責)</a><br />

     <a href="waist">褲子組立(40223104負責)</a><br />
     
     <a href="right_leg">右腳組立(40223104負責)</a><br />
     
     <a href="head">頭部組立(40223107負責)</a><br />
     
     <a href="hat">帽子組立(40223107負責)</a><br />
     
     <a href="left_leg">左腳組立(40223104負責)</a><br />
     
     
    <a href=" right_arm">右臂組立(40223102負責)</a><br />

    <a href=" right_hand">右手組立(40223102負責)</a><br />
     
     <a href=" left_arm">左臂組立(40223105負責)</a><br />
     
      <a href="left_hand">左手組立(40223105負責)</a><br />
     

    <a href="assembly">最終組立(40223131負責)</a><br />
    '''
        return outstring

    @cherrypy
.expose
    def assembly(self, *args, **kwargs):
            outstring = '''
    <!DOCTYPE html> 
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <script type="text/javascript" src="/static/weblink/examples/jscript/pfcUtils.js"></script>
    <script type="text/javascript
" src="/static/weblink/pfcUtils.js"></script>
    <script type="text/javascript" src="/static/weblink/wl_header.js"></script>
    </head>
    <body>
    </script><script language="JavaScript">

    /*man2.py 完全利用函式呼叫進行組立*/
    /*設計一個零件組立函式*/
    // featID 為組立件第一個組立零件的編號
    // inc 則為 part1 的組立順序編號, 第一個入組立檔編號為 featID+0
    // part2 為外加的零件名稱
    ////////////////////////////////////////////////
    // axis_plane_assembly 組立函式
    ////////////////////////////////////////////////
    function axis_plane_assembly(session, assembly, transf, featID, inc, part2, axis1, plane1, axis2, plane2){
    var descr = pfcCreate("pfcModelDescriptor").CreateFromFileName ("v:/home/lego/man/"+part2);
    var componentModel = session.GetModelFromDescr(descr);
    var componentModel = session.RetrieveModel(descr);
    if (componentModel != void null)
    {
        var asmcomp = assembly.AssembleComponent (componentModel, transf);
    }
    var ids = pfcCreate("intseq");
    ids.Append(featID+inc);
    var subPath = pfcCreate("MpfcAssembly").CreateComponentPath(assembly, ids);
    subassembly = subPath.Leaf;
    var asmDatums = new Array(axis1, plane1);
    var compDatums = new Array(axis2, plane2);
    var relation = new Array (pfcCreate("pfcComponentConstraintType").ASM_CONSTRAINT_ALIGN, pfcCreate("pfcComponentConstraintType").ASM_CONSTRAINT_MATE);
    var relationItem = new Array(pfcCreate("pfcModelItemType").ITEM_AXIS, pfcCreate("pfcModelItemType").ITEM_SURFACE);
    var constrs = pfcCreate("pfcComponentConstraints");
        for (var i = 0; i < 2; i++)
        {
            var asmItem = subassembly.GetItemByName (relationItem[i], asmDatums [i]);
            if (asmItem == void null)
            {
                interactFlag = true;
                continue;
            }
            var compItem = componentModel.GetItemByName (relationItem[i], compDatums [i]);
            if (compItem == void null)
            {
                interactFlag = true;
                continue;
            }
            var MpfcSelect = pfcCreate ("MpfcSelect");
            var asmSel = MpfcSelect.CreateModelItemSelection (asmItem, subPath);
            var compSel = MpfcSelect.CreateModelItemSelection (compItem, void null);
            var constr = pfcCreate("pfcComponentConstraint").Create (relation[i]);
            constr.AssemblyReference  = asmSel;
            constr.ComponentReference = compSel;
            constr.Attributes = pfcCreate("pfcConstraintAttributes").Create (true, false);
            constrs.Append(constr);
        }
    asmcomp.SetConstraints(constrs, void null);
    }
    // 以上為 axis_plane_assembly() 函式
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    // three_plane_assembly 採 align 組立, 若 featID 為 0 表示為空組立檔案
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    function three_plane_assembly(session, assembly, transf, featID, inc, part2, plane1, plane2, plane3, plane4, plane5, plane6){
    var descr = pfcCreate("pfcModelDescriptor").CreateFromFileName ("v:/home/lego/man/"+part2);
    var componentModel = session.GetModelFromDescr(descr);
    var componentModel = session.RetrieveModel(descr);
    if (componentModel != void null)
    {
        var asmcomp = assembly.AssembleComponent (componentModel, transf);
    }
    var ids = pfcCreate("intseq");
    // 若 featID 為 0 表示為空組立檔案
    if (featID != 0){
        ids.Append(featID+inc);
        var subPath = pfcCreate("MpfcAssembly").CreateComponentPath(assembly, ids);
        subassembly = subPath.Leaf;
        }else{
        var subPath = pfcCreate("MpfcAssembly").CreateComponentPath(assembly, ids);
        subassembly = assembly;
        // 設法取得第一個組立零件 first_featID
        // 取得 assembly 項下的元件 id, 因為只有一個零件, 採用 index 0 取出其 featID
        var components = assembly.ListFeaturesByType(true, pfcCreate ("pfcFeatureType").FEATTYPE_COMPONENT);
        // 此一 featID 為組立件中的第一個零件編號, 也就是樂高人偶的 body
        var first_featID = components.Item(0).Id;
        }
    var constrs = pfcCreate("pfcComponentConstraints");
    var asmDatums = new Array(plane1, plane2, plane3);
    var compDatums = new Array(plane4, plane5, plane6);
    var MpfcSelect = pfcCreate("MpfcSelect");
    for (var i = 0; i < 3; i++)
    {
        var asmItem = subassembly.GetItemByName(pfcCreate("pfcModelItemType").ITEM_SURFACE, asmDatums[i]);
        
        if (asmItem == void null)
        {
            interactFlag = true;
            continue;
        }
        var compItem = componentModel.GetItemByName(pfcCreate("pfcModelItemType").ITEM_SURFACE, compDatums[i]);
        if (compItem == void null)
        {
            interactFlag = true;
            continue;
        }
        var asmSel = MpfcSelect.CreateModelItemSelection(asmItem, subPath);
        var compSel = MpfcSelect.CreateModelItemSelection(compItem, void null);
        var constr = pfcCreate("pfcComponentConstraint").Create(pfcCreate("pfcComponentConstraintType").ASM_CONSTRAINT_ALIGN);
        constr.AssemblyReference = asmSel;
        constr.ComponentReference = compSel;
        constr.Attributes = pfcCreate("pfcConstraintAttributes").Create (false, false);
        constrs.Append(constr);
    }
    asmcomp.SetConstraints(constrs, void null);
    // 若 featID = 0 則傳回 first_featID
    if (featID == 0)
        return first_featID;
    }
    // 以上為 three_plane_assembly() 函式
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    // three_plane_assembly2 採 mate 組立, 若 featID 為 0 表示為空組立檔案
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    function three_plane_assembly2(session, assembly, transf, featID, inc, part2, plane1, plane2, plane3, plane4, plane5, plane6){
    var descr = pfcCreate("pfcModelDescriptor").CreateFromFileName ("v:/home/lego/man/"+part2);
    var componentModel = session.GetModelFromDescr(descr);
    var componentModel = session.RetrieveModel(descr);
    if (componentModel != void null)
    {
        var asmcomp = assembly.AssembleComponent (componentModel, transf);
    }
    var ids = pfcCreate("intseq");
    // 若 featID 為 0 表示為空組立檔案
    if (featID != 0){
        ids.Append(featID+inc);
        var subPath = pfcCreate("MpfcAssembly").CreateComponentPath(assembly, ids);
        subassembly = subPath.Leaf;
        }else