php读取dxf,分享个DXF转G代码的实例
import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=469;acceptfiles=1)
winform.add(
button={cls="button";text='\u2460獲取有效數據';left=609;top=363;right=757;bottom=466;font=LOGFONT(h=-21);z=2};
button2={cls="button";text='\u2461生成ARC';left=5;top=363;right=138;bottom=466;font=LOGFONT(h=-21);z=3};
button3={cls="button";text='\u2464生成正序G';left=304;top=363;right=436;bottom=466;font=LOGFONT(h=-21);z=6};
button4={cls="button";text='\u2462排序';left=139;top=363;right=221;bottom=466;font=LOGFONT(h=-21);z=4};
button5={cls="button";text='\u2463結果';left=225;top=363;right=300;bottom=466;font=LOGFONT(h=-21);z=5};
button6={cls="button";text="生成反序G";left=442;top=363;right=579;bottom=466;font=LOGFONT(h=-21);z=7};
edit={cls="edit";left=0;top=0;right=760;bottom=362;edge=1;multiline=1;vscroll=1;z=1}
)
/*}}*/
var filePath,fileContents;
import console;
console.open()
winform.wndproc = function(hwnd,message,wParam,lParam){
select( message ) {
//拖拽文件到窗體里
case 0x233/*_WM_DROPFILES*/{
filePath = win.getDropFile(wParam )[1];
}
else{
}
}
}
class entitiesClass{
line = {};
arc = {};
}
class line{
//線段的第一個點
float x1 = 0.0;
float y1 = 0.0;
float z1 = 0.0;
//線段的第二個點
float x2 = 0.0;
float y2 = 0.0;
float z2 = 0.0;
};
class arc{
//弧形的第一個點
float x1 = 0.0;
float y1 = 0.0;
float z1 = 0.0;
//弧形第二個點
float x2 = 0.0;
float y2 = 0.0;
float z2 = 0.0;
//弧形半徑
float R = 0.0;
//弧形圓心點
float xx = 0.0;
float yy = 0.0;
float zz = 0.0;
//弧形起始角度和終止角度
float startN = 0.0;
float endN = 0.0;
};
var G_line = line();
var G_arc = arc();
var entitiesTab = entitiesClass();
var entitiesStart = false;
winform.button.oncommand = function(id,event){
winform.edit.text = "";
fileContents = io.open(filePath,"r+");
fileContents.seek("set");
do{
//讀取文件的一行
var stringLine = tostring(fileContents.read());
//去除該行的首尾空白字符之后進行判斷
select( string.trim( stringLine ) ) {
case "ENTITIES" {
console.log("找到實體文件! 開始解析:");
entitiesStart = true;
}
case "LINE" {
//只有找到實體線條模塊之后,線條類型解析才有效
if(entitiesStart == true){
//循環讀取直到找到線屬性這行
while( fileContents.read() != "AcDbLine" ){? ? ? ? };
//LINE線屬性共有6組,其中每組第一行是特定屬性名,第二行是屬性值
for(i=1;6;1){
select( string.trim(tostring( fileContents.read() )) ) {
case "10" {
var pos = fileContents.read();
G_line.x1 = tonumber( string.format("%.3f",pos ) );
}
case "20" {
var pos = fileContents.read();
G_line.y1 = tonumber( string.format("%.3f",pos ) );
}
case "30" {
var pos = fileContents.read();
G_line.z1 = tonumber( string.format("%.3f",pos ) );
}
case "11" {
var pos = fileContents.read();
G_line.x2 = tonumber( string.format("%.3f",pos ) );
}
case "21" {
var pos = fileContents.read();
G_line.y2 = tonumber( string.format("%.3f",pos ) );
}
case "31" {
var pos = fileContents.read();
G_line.z2 = tonumber( string.format("%.3f",pos ) );;
}
else {
}
}
}
//每塊LINE類型屬性讀取完畢,放入LINE表中,等待下一步操作
table.push(entitiesTab.line,table.clone(G_line));
}
}
case "ARC" {
if(entitiesStart == true){
//循環讀取直到找到線屬性這行
while( fileContents.read() != "AcDbCircle" ){? ? ? ? };
//ARC弧線屬性共有6組,其中每組第一行是特定屬性名,第二行是屬性值
for(i=1;6;1){
select( string.trim(tostring( fileContents.read() )) ) {
case "10" {
var pos = fileContents.read();
G_arc.xx = tonumber( string.format("%.3f",pos ) );
}
case "20" {
var pos = fileContents.read();
G_arc.yy = tonumber( string.format("%.3f",pos ) );
}
case "30" {
var pos = fileContents.read();
G_arc.zz = tonumber( string.format("%.3f",pos ) );
}
case "40" {
var pos = fileContents.read();
G_arc.R = tonumber( string.format("%.3f",pos ) );
while( fileContents.read() != "AcDbArc" ){? ? ? ? };
}
case "50" {
var pos = fileContents.read();
G_arc.startN = tonumber( string.format("%.3f",pos ) );
}
case "51" {
var pos = fileContents.read();
G_arc.endN = tonumber( string.format("%.3f",pos ) );
}
else {
}
}
}
//每塊ARC類型屬性讀取完畢,放入ARC表中,等待下一步操作
table.push(entitiesTab.arc,table.clone(G_arc));
}
}
//其他模塊暫時不使用,檢測到此模塊即結束此次解析
case "CIRCLE","ELLIPSE","POLYLINE","VERTEX","SPLINE","INDERT","TEXT","DIMENSION" {
if(entitiesStart == true){
entitiesStart = false;
}
}
//文件末尾
case "EOF" {
console.dump(entitiesTab);
}
else {
}
}
}while( fileContents.read(0)/**是否是最后一行**/ )
}
//解析獲得的ARC數據,求出弧線的起始點坐標和終止點坐標
winform.button2.oncommand = function(id,event){
for(i=1;#entitiesTab.arc;1){
if( (entitiesTab.arc[ i ].startN >= 0) && (entitiesTab.arc[ i ].startN <= 90) ){
//起始點在第一象限
entitiesTab.arc[ i ].x1 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].R * math.cos(math.rad(entitiesTab.arc[ i ].startN)) + entitiesTab.arc[ i ].xx) );
entitiesTab.arc[ i ].y1 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].R * math.sin(math.rad(entitiesTab.arc[ i ].startN)) + entitiesTab.arc[ i ].yy) );
}elseif( (entitiesTab.arc[ i ].startN > 90) && (entitiesTab.arc[ i ].startN <= 180) ){
//起始點在第二象限
entitiesTab.arc[ i ].x1 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].xx - entitiesTab.arc[ i ].R * math.cos(math.rad(180 - entitiesTab.arc[ i ].startN))) );
entitiesTab.arc[ i ].y1 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].R * math.sin(math.rad(180 - entitiesTab.arc[ i ].startN)) + entitiesTab.arc[ i ].yy) );
}elseif( (entitiesTab.arc[ i ].startN > 180) && (entitiesTab.arc[ i ].startN <= 270) ){
//起始點在第三象限
entitiesTab.arc[ i ].x1 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].xx - entitiesTab.arc[ i ].R * math.cos(math.rad(entitiesTab.arc[ i ].startN - 180))) );
entitiesTab.arc[ i ].y1 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].yy - entitiesTab.arc[ i ].R * math.sin(math.rad(entitiesTab.arc[ i ].startN - 180))) );
}elseif( (entitiesTab.arc[ i ].startN > 270) && (entitiesTab.arc[ i ].startN <=360) ){
//起始點在第四象限
entitiesTab.arc[ i ].x1 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].xx + entitiesTab.arc[ i ].R * math.cos(math.rad(360 - entitiesTab.arc[ i ].startN))) );
entitiesTab.arc[ i ].y1 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].yy - entitiesTab.arc[ i ].R * math.sin(math.rad(360 - entitiesTab.arc[ i ].startN))) );
}
if( (entitiesTab.arc[ i ].endN >= 0) && (entitiesTab.arc[ i ].endN <= 90) ){
//終止點在第一象限
entitiesTab.arc[ i ].x2 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].R * math.cos(math.rad(entitiesTab.arc[ i ].endN)) + entitiesTab.arc[ i ].xx) );
entitiesTab.arc[ i ].y2 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].R * math.sin(math.rad(entitiesTab.arc[ i ].endN)) + entitiesTab.arc[ i ].yy) );
}elseif( (entitiesTab.arc[ i ].endN > 90) && (entitiesTab.arc[ i ].endN <= 180) ){
//終止點在第二象限
entitiesTab.arc[ i ].x2 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].xx - entitiesTab.arc[ i ].R * math.cos(math.rad(180 - entitiesTab.arc[ i ].endN))) );
entitiesTab.arc[ i ].y2 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].R * math.sin(math.rad(180 - entitiesTab.arc[ i ].endN)) + entitiesTab.arc[ i ].yy) );
}elseif( (entitiesTab.arc[ i ].endN > 180) && (entitiesTab.arc[ i ].endN <= 270) ){
//終止點在第三象限
entitiesTab.arc[ i ].x2 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].xx - entitiesTab.arc[ i ].R * math.cos(math.rad(entitiesTab.arc[ i ].endN - 180))) );
entitiesTab.arc[ i ].y2 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].yy - entitiesTab.arc[ i ].R * math.sin(math.rad(entitiesTab.arc[ i ].endN - 180))) );
}elseif( (entitiesTab.arc[ i ].endN > 270) && (entitiesTab.arc[ i ].endN <=360) ){
//終止點在第四象限
entitiesTab.arc[ i ].x2 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].xx + entitiesTab.arc[ i ].R * math.cos(math.rad(360 - entitiesTab.arc[ i ].endN))) );
entitiesTab.arc[ i ].y2 = tonumber( string.format("%.3f", entitiesTab.arc[ i ].yy - entitiesTab.arc[ i ].R * math.sin(math.rad(360 - entitiesTab.arc[ i ].endN))) );
}
}
}
//定義排序列表
class retClass{
str G_type;? ? ? ? ? ? ? ? ? ? ? ? //類型
point G_content;? ? ? ? //內容
}
class lineClass{
float startX1;
float startY1;
float endX1;
float endY1;
}
class arcClass{
float R;
float startX1;
float startY1;
float endX1;
float endY1;
float centerX;
float centerY;
}
var retTable = {};
var retType = retClass();
var lineType = lineClass();
var arcType = arcClass();
var 正常順序 = function( tab , flag ){
if(flag == "line"){
retType.G_type = "line";
lineType.startX1 = tab.x1;
lineType.startY1 = tab.y1;
lineType.endX1 = tab.x2;
lineType.endY1 = tab.y2;
retType.G_content = table.clone( lineType );
}elseif( flag == "arc" ){
retType.G_type = "逆時針";
arcType.startX1 = tab.x1;
arcType.startY1 = tab.y1;
arcType.endX1 = tab.x2;
arcType.endY1 = tab.y2;
arcType.centerX = tab.xx;
arcType.centerY = tab.yy;
arcType.R = tab.R;
retType.G_content = table.clone( arcType );
}
return retType;
}
var 顛倒順序 = function( tab , flag ){
if(flag == "line"){
retType.G_type = "line";
lineType.startX1 = tab.x2;
lineType.startY1 = tab.y2;
lineType.endX1 = tab.x1;
lineType.endY1 = tab.y1;
retType.G_content = table.clone( lineType );
}elseif( flag == "arc" ){
retType.G_type = "順時針";
arcType.startX1 = tab.x2;
arcType.startY1 = tab.y2;
arcType.endX1 = tab.x1;
arcType.endY1 = tab.y1;
arcType.centerX = tab.xx;
arcType.centerY = tab.yy;
arcType.R = tab.R;
retType.G_content = table.clone( arcType );
}
return retType;
}
winform.button4.oncommand = function(id,event){
/*
取出排好序的表的第一元素的頭點坐標,和最后一個元素的尾坐標,
拿其他沒有對比過的線條的頭坐標和尾坐標分別比較,然后就可以插入到相應的排序表的頭和尾部
這樣就形成一個連續的隊列
難點在對比并放入排序列表的線條,應該放入后從原表中刪除,防止多次重復對比,因為每根線條只有唯一的序號,無需再次判斷
table貌似沒有刪除哪一條的指令
*/
//首先把第一個線條作為第一序列
//console.dump(entitiesTab.line[1])
var tempTab = 正常順序(entitiesTab.line[1],"line");
table.push(retTable, table.clone(tempTab) );
table.remove(entitiesTab.line,1);
while( (#entitiesTab.arc + #entitiesTab.line) ){
for(k=1;#entitiesTab.arc;1){
if((retTable[1].G_content.startX1 == entitiesTab.arc[k].x1) && (retTable[1].G_content.startY1 == entitiesTab.arc[k].y1)){
var tempTab = 顛倒順序(entitiesTab.arc[k],"arc");
table.insert(retTable,table.clone(tempTab));//插到前面
table.remove(entitiesTab.arc,k);
break 1;
}
if((retTable[1].G_content.startX1 == entitiesTab.arc[k].x2) && (retTable[1].G_content.startY1 == entitiesTab.arc[k].y2)){
var tempTab = 正常順序(entitiesTab.arc[k],"arc");
table.insert(retTable,table.clone(tempTab));//插到前面
table.remove(entitiesTab.arc,k);
break 1;
}
if((retTable[#retTable].G_content.endX1 == entitiesTab.arc[k].x1) && (retTable[#retTable].G_content.endY1 == entitiesTab.arc[k].y1)){
var tempTab = 正常順序(entitiesTab.arc[k],"arc");
table.push(retTable,table.clone(tempTab));//插到后面
table.remove(entitiesTab.arc,k);
break 1;
}
if((retTable[#retTable].G_content.endX1 == entitiesTab.arc[k].x2) && (retTable[#retTable].G_content.endY1 == entitiesTab.arc[k].y2)){
var tempTab = 顛倒順序(entitiesTab.arc[k],"arc");
table.push(retTable,table.clone(tempTab));//插到后面
table.remove(entitiesTab.arc,k);
break 1;
}
}
//-----------------------------------------------------
for(j=1;#entitiesTab.line;1){
//起始點有沒重合
if((retTable[1].G_content.startX1 == entitiesTab.line[j].x1) && (retTable[1].G_content.startY1 == entitiesTab.line[j].y1)){
var tempTab = 顛倒順序(entitiesTab.line[j],"line");
table.insert(retTable,table.clone(tempTab));//插到前面
table.remove(entitiesTab.line,j);
break 1;
}
if((retTable[1].G_content.startX1 == entitiesTab.line[j].x2) && (retTable[1].G_content.startY1 == entitiesTab.line[j].y2)){
var tempTab = 正常順序(entitiesTab.line[j],"line");
table.insert(retTable,table.clone(tempTab));//插到前面
table.remove(entitiesTab.line,j);
break 1;
}
if((retTable[#retTable].G_content.endX1 == entitiesTab.line[j].x1) && (retTable[#retTable].G_content.endY1 == entitiesTab.line[j].y1)){
var tempTab = 正常順序(entitiesTab.line[j],"line");
table.push(retTable,table.clone(tempTab));//插到后面
table.remove(entitiesTab.line,j);
break 1;
}
if((retTable[#retTable].G_content.endX1 == entitiesTab.line[j].x2) && (retTable[#retTable].G_content.endY1 == entitiesTab.line[j].y2)){
var tempTab = 顛倒順序(entitiesTab.line[j],"line");
table.push(retTable,table.clone(tempTab));//插到后面
table.remove(entitiesTab.line,j);
break 1;
}
}
}
console.log("轉換完成!");
}
winform.button5.oncommand = function(id,event){
console.dump(retTable)
}
winform.button3.oncommand = function(id,event){
var GcodeStr;
for(k,v in retTable){
if(GcodeStr == null){
GcodeStr = string.concat(GcodeStr,"G00 ","X",v.G_content.startX1," Y",v.G_content.startY1,'\r\n');
}
select(v.G_type) {
case "line" {
GcodeStr = string.concat(GcodeStr,"G01 ","X",v.G_content.endX1," Y",v.G_content.endY1,'\r\n');
}
case "順時針" {
GcodeStr = string.concat(GcodeStr,"G02 ","X",v.G_content.endX1," Y",v.G_content.endY1," R",v.G_content.R,'\r\n');
}
case "逆時針" {
GcodeStr = string.concat(GcodeStr,"G03 ","X",v.G_content.endX1," Y",v.G_content.endY1," R",v.G_content.R,'\r\n');
}
else {
console.log("rrrrrrrrrrrrr")
}
}
}
console.log( GcodeStr );
string.save("/cnctest.nc",GcodeStr)
}
winform.button6.oncommand = function(id,event){
var GcodeStr;
table.reverse(retTable);
for(k,v in retTable){
if(GcodeStr == null){
GcodeStr = string.concat(GcodeStr,"G00 ","X",v.G_content.endX1," Y",v.G_content.endY1,'\r\n');
}
select(v.G_type) {
case "line" {
GcodeStr = string.concat(GcodeStr,"G01 ","X",v.G_content.startX1," Y",v.G_content.startY1,'\r\n');
}
case "順時針" {
GcodeStr = string.concat(GcodeStr,"G03 ","X",v.G_content.startX1," Y",v.G_content.startY1," R",v.G_content.R,'\r\n');
}
case "逆時針" {
GcodeStr = string.concat(GcodeStr,"G02 ","X",v.G_content.startX1," Y",v.G_content.startY1," R",v.G_content.R,'\r\n');
}
else {
console.log("rrrrrrrrrrrrr")
}
}
}
console.log( GcodeStr );
string.save("/cnctestFFFF.nc",GcodeStr)
}
winform.show()
win.loopMessage();
總結
以上是生活随笔為你收集整理的php读取dxf,分享个DXF转G代码的实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 8k分辨率是多少像素(详解分辨率1080
- 下一篇: 忍者大师晓鼬最强阵容是什么