Altium Designer使用JavaScript自动添加Netlabel

      前面一篇文章提到使用脚本系统加速Altium Designer设计,这里贴出具体的实现。

      前提是我们使用Candance FPGA System Planner生成了Netlist文件,格式如下:

      

      

      接下来,我们使用JavaScript自动搜索对应的FPGA管脚,自动添加Netlabel,并在管脚上添加定长的导线,代码如下:

      

/*******************Constants****************************/
var WIRE_LEN=300;
var NET_LABEL_TEXT_OFFSET_ROTATE180=1000;
var CHAR_GRAPHICAL_LENGTH=60;
var NETLIST_FILE_PATH="C:\\SPB_Data\\fsp_working\\Switch_prj_v4_55\\output\\netlist.txt";
var COMPONENT_NAME="FPGA1";
var SPLIT_REGEX=/[ ;']+/;
/*******************End of Constants*********************/

var SchDoc;
var SchIterator;
var SchComponent;
var Pin;
var PinIterator;
var Location;
var pinDesignatorStr;
var LocX;
var LocY;
var Len;
var dict = new Array();
var DetaX=new Array(1,0,-1,0);
var DetaY=new Array(0,1,0,-1);

//-----------------------------------------------------------
function main(){
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var f = fso.OpenTextFile(NETLIST_FILE_PATH,1);
    var f2 = fso.OpenTextFile("out.txt",2,true);
    var s = "";
    s=f.ReadLine();
    while (!f.AtEndOfStream){
        s=f.ReadLine();
        ss=s.split(SPLIT_REGEX);
        for(var i=1;i<ss.length;i++){
            dict[ss[i]]=ss[0];
        }
    }
    //f2.WriteLine(dict["FPGA2.J27"]);
    f.Close();
    f2.Close();

    if(SchServer==null) return;
    SchDoc=SchServer.GetCurrentSchDocument();
    if(SchDoc==null) return;
    SchIterator = SchDoc.SchIterator_Create();
    SchIterator.AddFilter_ObjectSet(MkSet(eSchComponent));
    SchComponent = SchIterator.FirstSchObject();
    while(SchComponent!=null){
        PinIterator = SchComponent.SchIterator_Create;
        PinIterator.AddFilter_ObjectSet(MkSet(ePin));
        PinIterator.AddFilter_CurrentPartPrimitives();
        Pin = PinIterator.FirstSchObject();
        while(Pin!=null){
            Location = Pin.GetState_Location();
            Len  = Pin.PinLength / cInternalPrecision;
            LocX=Location.X / cInternalPrecision+Len*DetaX[Pin.Orientation];
            LocY=Location.Y / cInternalPrecision+Len*DetaY[Pin.Orientation];
            pinDesignatorStr=Pin.Designator;
            pinDesignatorStr=COMPONENT_NAME+"."+pinDesignatorStr;
            if(pinDesignatorStr in dict){
                PlaceASchNetLabel(LocX,LocY,pinDesignatorStr,Pin.Orientation);
            }
            PlaceASchWire(LocX,LocY,Pin.Orientation);
            Pin = PinIterator.NextSchObject();
        }
        SchComponent = SchIterator.NextSchObject();
    }
    SchDoc.GraphicallyInvalidate();
    ResetParameters;
    AddStringParameter('Action', 'Redraw');
    RunProcess('Sch:Zoom');
    showmessage("done!");
    return;
}


function PlaceASchNetLabel(x,y,text,rotate){
    var SchNetlabel;
    SchNetlabel = SchServer.SchObjectFactory(eNetlabel,eCreate_GlobalCopy);
    if (SchNetlabel == null) return;
    SchNetlabel.Text = dict[text];
    switch(rotate){
        case eRotate180:{
            // x=x-NET_LABEL_TEXT_OFFSET_ROTATE180-SchNetlabel.Text.length*CHAR_GRAPHICAL_LENGTH;
            x=x-SchNetlabel.Text.length*CHAR_GRAPHICAL_LENGTH;
            rotate=eRotate0;
            break;
        }
        case eRotate270:{
            // y=y-NET_LABEL_TEXT_OFFSET_ROTATE180-SchNetlabel.Text.length*CHAR_GRAPHICAL_LENGTH;
            y=y-SchNetlabel.Text.length*CHAR_GRAPHICAL_LENGTH;
            rotate=eRotate90;
            break;
        }
        default:;
    }
    SchNetlabel.MoveToXY(MilsToCoord(x), MilsToCoord(y));
    SchNetlabel.RotateBy90(Point(MilsToCoord(x), MilsToCoord(y)), rotate);
    SchNetlabel.SetState_xSizeySize;
    SchNetlabel.GraphicallyInvalidate();
    SchDoc.RegisterSchObjectInContainer(SchNetlabel);

    //SchDoc.AddSchObject(SchNetlabel);
    //SchServer.RobotManager.SendMessage(SchNetlabel.I_ObjectAddress, c_BroadCast, SCHM_BeginModify, c_NoEventData);
    //SchServer.RobotManager.SendMessage(SchNetlabel.I_ObjectAddress, c_BroadCast, SCHM_EndModify, c_NoEventData);
    //SchServer.RobotManager.SendMessage(SchDoc.I_ObjectAddress,c_BroadCast,SCHM_PrimitiveRegistration,SchNetlabel.I_ObjectAddress);

}

function PlaceASchWire(x,y,rotate){
    var SchWire;
    SchWire = SchServer.SchObjectFactory(eWire,eCreate_GlobalCopy);
    if (SchWire == null) return;
    SchWire.Location = Point(MilsToCoord(x), MilsToCoord(y));
    SchWire.InsertVertex = 1;
    SchWire.SetState_Vertex(1, Point(MilsToCOord(x), MilsToCoord(y)));
    SchWire.InsertVertex = 1;
    SchWire.SetState_Vertex(1, Point(MilsToCOord(x+WIRE_LEN*DetaX[rotate]), MilsToCoord(y+WIRE_LEN*DetaY[rotate])));
    SchDoc.RegisterSchObjectInContainer(SchWire);

}

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页