加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Matlab产生IGES文件代码

(2011-05-07 12:53:46)
标签:

杂谈

分类: Matlab

发现有人也在研究IGES文件格式,那就把之前编写的Matlab将数据写入IGES文件的代码上传吧,供大家参考。之前博客见

CAD/CAM软件与Matlab基于IGES文件的数据通讯http://blog.sina.com.cn/s/blog_6163bdeb0100o85m.html

 

代码转载请注明作者。

代码如下,看看当初写的代码,好稚嫩啊

main脚本文件

%产生数据并写入iges文件
clc
clear

issearch=2;%0表示只写点,1表示只写线,2表示点线都写

fprintf('正在写文件。。。\n');
%产生正弦波文件
x=0:0.1:10;
x=x';
y=sin(x);
Data=[x y];
Data(1,3)=0;
write_iges('iges_sin.igs',Data,issearch)

%产生peaks文件
[x,y,z]=peaks(30);
xx=x(:);
yy=y(:);
zz=z(:);
Data=[xx zz yy];
write_iges('iges_peaks.igs',Data,issearch)

%产生抛物线文件
[x,y]=meshgrid(-1:0.1:1);
z=x.^2+y.^2;
xx=x(:);
yy=y(:);
zz=z(:);
Data=[xx yy zz];
write_iges('iges_paowu.igs',Data,issearch)

%产生解释文件
xx=[10;0];
yy=[0;10];
zz=[0;0];
Data=[xx yy zz];
write_iges('igesforexplain.igs',Data,0)

%产生Matlab图标
[x,y]=meshgrid(linspace(-1,1,16));
z=membrane;
z=z(1:2:end,1:2:end,1:2:end);
xx=x(:);
yy=y(:);
zz=z(:);
Data=[xx zz yy];
write_iges('iges_matlab.igs',Data,issearch)
fprintf('写文件结束。\n');

 

add_start_global函数文件:

%构造开始段和全局段字符串
function [filename,file_str]=add_start_global(FileName)
    %开始段信息
    start='Matlab-IGES-UG,Proe';
    %全局段信息
    timedata=clock;
    Time=sprintf('%4s%2s%2s.%2s%2s%2s',num2str(timedata(1)),num2str(timedata(2)),num2str(timedata(3)),num2str(timedata(4)),num2str(timedata(5)),num2str(floor(timedata(6))));
    Author='USTC GJ_QSQ';
    G_1='1H,';
    G_2='1H;';
    G_3='7HPRT0001';
    G_4=strcat(num2str(length(FileName)),'H',FileName);     %FileName
    G_5='49HPro/ENGINEER by Parametric Technology Corporation';
    G_6='7H2007170';
    G_7='32';
    G_8='38';
    G_9='7';
    G_10='38';
    G_11='15';
    G_12='7HPRT0001';
    G_13='1.';
    G_14='2';
    G_15='2HMM';
    G_16='32768';
    G_17='0.5';
    G_18=strcat(num2str(length(Time)),'H',Time);     %Time;
    G_19='0.0865991';
    G_20='866.025';
    G_21=strcat(num2str(length(Author)),'H',Author);     %Author;
    G_22='4HUSTC';
    G_23='10';
    G_24='0';
    G_25=strcat(num2str(length(Time)),'H',Time);     %Time;
   
    %构造字符串
    file_str='';
    %start
    start=add_section_index(start,'S',1);
    file_str=strcat(file_str,start,'\n');
    %global
    rowstr='';
    count=0;
    for i=1:25
        if i==25
            temp=strcat((['G_',num2str(i)]),';');
        else
            temp=strcat((['G_',num2str(i)]),',');
        end

        if length(rowstr)+length(temp)>72
            count=count+1;
            rowstr=add_section_index(rowstr,'G',count);
            file_str=strcat(file_str,rowstr,'\n');
            rowstr=temp;
        else
            rowstr=strcat(rowstr,temp);
            if i==25
                count=count+1;
                rowstr=add_section_index(rowstr,'G',count);
                file_str=strcat(file_str,rowstr,'\n');
            end
        end
    end
    filename=FileName;
end


function result=add_section_index(str,section,index)
    result=sprintf('%-72s%s}',str,section,index);
end

 

write_iges函数文件:

%写igs文件
%FileName为写入的文件名,Data为待写入的数据
function write_iges(FileName,Data,PointOrLine)
    Index=0;
    %构造开始段和全局段字符串
    [filename,file_str]=add_start_global(FileName);   
   
    %构造索引段和参数段
        if nargin==2
            pointorline=2;%0表示只写点,1表示只写线,2表示点线都写
        else
            pointorline=PointOrLine;
        end       
        if pointorline==0
            %写点
            [strDir2 strPara2 Index]= writePoint(Data(1:end,1),Data(1:end,2),Data(1:end,3),Index);
            file_str=strcat(file_str,strDir2);
            file_str=strcat(file_str,strPara2);      
        elseif pointorline==1
            %写线
            [strDir1 strPara1 Index]= writeLine(Data(1:end-1,1),Data(1:end-1,2),Data(1:end-1,3),Data(2:end,1),Data(2:end,2),Data(2:end,3),Index);
            file_str=strcat(file_str,strDir1);
            file_str=strcat(file_str,strPara1);           
        elseif pointorline==2
            %写线
            [strDir1 strPara1 Index]= writeLine(Data(1:end-1,1),Data(1:end-1,2),Data(1:end-1,3),Data(2:end,1),Data(2:end,2),Data(2:end,3),Index);
            file_str=strcat(file_str,strDir1);
            %写点
            [strDir2 strPara2 Index]= writePoint(Data(1:end,1),Data(1:end,2),Data(1:end,3),Index);
            file_str=strcat(file_str,strDir2);

            file_str=strcat(file_str,strPara1);
            file_str=strcat(file_str,strPara2);           
        end       
   
    %构造结束段
    str_end=sprintf('%s}%s}%s}%s}As}','S',1,'G',4,'D',2*Index,'P',Index,'T',1);
    file_str=strcat(file_str,str_end,'\n');
   
    %写入文件
    fid=fopen(FileName,'wt');
    fprintf(fid,file_str);
    fclose(fid);
end

 

writeLine函数文件:

%Index为总元素的个数
function [strDir, strPara, Index] = writeLine(x1, y1, z1, x2, y2, z2, Index)
if length(x1)~=length(y1)||length(x1)~=length(z1)||length(x2)~=length(y2)||length(x2)~=length(z2)||length(x2)~=length(y1)
    disp('2了,几个线呀!');
end
Direction=struct('type',[],...%
    'paraPtr',[],...%
    'version','1',...
    'lineStyle','1',...
    'layer','0',...
    'view','0',...
    'matrix','0',...
    'label','0',...
    'status','00000001',...
    'section','D',...
    'type1',[],...%
    'lineWeight','0',...
    'color','2',...
    'rowNum','1',...
    'form','0',...
    'reserved','',...
    'reserved1','',...
    'elementLabel',[],...%
    'elementIndex',[],...%
    'section1','D'...
    );

Line=struct('pointDir',Direction,...
    'x1',[],...
    'y1',[],...
    'z1',[],...
    'x2',[],...
    'y2',[],...
    'z2',[],...
    'ptr',[]...
    );

count=0;
strDir='';
strPara='';
for i=1:length(x1)
    count=count+1;
    Index=Index+1;
    Line.pointDir.type='110';
    Line.pointDir.elementLabel='LINE';
    Line.pointDir.elementIndex=num2str(count);
    Line.pointDir.paraPtr=num2str(Index);
   
    Line.pointDir.type1=Line.pointDir.type;
   
    Line.x1=x1;
    Line.y1=y1;
    Line.z1=z1;
    Line.x2=x2;
    Line.y2=y2;
    Line.z2=z2;
    Line.ptr=2*Index-1;
   
    %构造字符串
    strDir_temp=sprintf('%8s%8s%8s%8s%8s%8s%8s%8s%8s%s}\n%8s%8s%8s%8s%8s%8s%8s%8s%8s%s}',...
        Line.pointDir.type,...
        Line.pointDir.paraPtr,...
        Line.pointDir.version,...
        Line.pointDir.lineStyle,...
        Line.pointDir.layer,...
        Line.pointDir.view,...
        Line.pointDir.matrix,...
        Line.pointDir.label,...
        Line.pointDir.status,...
        Line.pointDir.section,2*Index-1,...
        Line.pointDir.type1,...
        Line.pointDir.lineWeight,...
        Line.pointDir.color,...
        Line.pointDir.rowNum,...
        Line.pointDir.form,...
        Line.pointDir.reserved,...
        Line.pointDir.reserved1,...
        Line.pointDir.elementLabel,...
        Line.pointDir.elementIndex,...
        Line.pointDir.section1,2*Index);
    strPara_temp=sprintf('%s,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f;',...
        Line.pointDir.type,...
        Line.x1(i),...
        Line.y1(i),...
        Line.z1(i),...
        Line.x2(i),...
        Line.y2(i),...
        Line.z2(i));
    strPara_temp=sprintf('%-64s�P%7s',strPara_temp,2*Index-1,Line.pointDir.paraPtr);
   
    strDir=strcat(strDir,strDir_temp,'\n');
    strPara=strcat(strPara,strPara_temp,'\n');
end
end

 

writePoint函数文件:

%Index为总元素的个数
function [strDir, strPara, Index] = writePoint(x, y, z, Index)
if length(x)~=length(y)||length(x)~=length(z)
    disp('2了,几个点呀!');
end
Direction=struct('type',[],...%
    'paraPtr',[],...%
    'version','1',...
    'lineStyle','1',...
    'layer','0',...
    'view','0',...
    'matrix','0',...
    'label','0',...
    'status','00000001',...
    'section','D',...
    'type1',[],...%
    'lineWeight','0',...
    'color','2',...
    'rowNum','1',...
    'form','0',...
    'reserved','',...
    'reserved1','',...
    'elementLabel',[],...%
    'elementIndex',[],...%
    'section1','D'...
    );

Point=struct('pointDir',Direction,...
    'x',[],...
    'y',[],...
    'z',[],...
    'ptr',[]...
    );

count=0;
strDir='';
strPara='';
for i=1:length(x)
    count=count+1;
    Index=Index+1;
    Point.pointDir.type='116';
    Point.pointDir.elementLabel='POINT';
    Point.pointDir.elementIndex=num2str(count);
    Point.pointDir.paraPtr=num2str(Index);
   
    Point.pointDir.type1=Point.pointDir.type;
   
    Point.x=x;
    Point.y=y;
    Point.z=z;
    Point.ptr=2*Index-1;
   
    %构造字符串
    strDir_temp=sprintf('%8s%8s%8s%8s%8s%8s%8s%8s%8s%s}\n%8s%8s%8s%8s%8s%8s%8s%8s%8s%s}',...
        Point.pointDir.type,...
        Point.pointDir.paraPtr,...
        Point.pointDir.version,...
        Point.pointDir.lineStyle,...
        Point.pointDir.layer,...
        Point.pointDir.view,...
        Point.pointDir.matrix,...
        Point.pointDir.label,...
        Point.pointDir.status,...
        Point.pointDir.section,2*Index-1,...
        Point.pointDir.type1,...
        Point.pointDir.lineWeight,...
        Point.pointDir.color,...
        Point.pointDir.rowNum,...
        Point.pointDir.form,...
        Point.pointDir.reserved,...
        Point.pointDir.reserved1,...
        Point.pointDir.elementLabel,...
        Point.pointDir.elementIndex,...
        Point.pointDir.section1,2*Index);
    strPara_temp=sprintf('%s,%.2f,%.2f,%.2f;',...
        Point.pointDir.type,...
        Point.x(i),...
        Point.y(i),...
        Point.z(i));
    strPara_temp=sprintf('%-64s?P%7s',strPara_temp,2*Index-1,Point.pointDir.paraPtr);
   
    strDir=strcat(strDir,strDir_temp,'\n');
    strPara=strcat(strPara,strPara_temp,'\n');
end
end

 

writeColor函数文件:

%Index为总元素的个数
function [strDir, strPara, Index] = writeColor(r, g, b, Index)
    if length(r)~=length(g)||length(r)~=length(b)
        disp('2了,几个点呀!');
    end
    Direction=struct('type',[],...%
                'paraPtr',[],...%
                'version','1',...
                'lineStyle','1',...
                'layer','0',...
                'view','0',...
                'matrix','0',...
                'label','0',...
                'status','01000200',...
                'section','D',...
                'type1',[],...%
                'lineWeight','0',...
                'color','2',...
                'rowNum','1',...
                'form','0',...
                'reserved','',...
                'reserved1','',...
                'elementLabel',[],...%
                'elementIndex',[],...%
                'section1','D'...
            );
               
    Color=struct('pointDir',Direction,...
            'r',[],...
            'g',[],...
            'b',[],...
            'ptr',[]...
            );
       
    count=0;
    strDir='';
    strPara='';
    for i=1:length(r)
        count=count+1;
        Index=Index+1;
        Color.pointDir.type='314';
        Color.pointDir.elementLabel='COLOR';
        Color.pointDir.elementIndex=num2str(count);
        Color.pointDir.paraPtr=num2str(Index);

        Color.pointDir.type1=Color.pointDir.type; 

        Color.r=r;
        Color.g=g;
        Color.b=b;
        Color.ptr=2*Index-1;

    %构造字符串
        strDir_temp=sprintf('%8s%8s%8s%8s%8s%8s%8s%8s%8s%s}\n%8s%8s%8s%8s%8s%8s%8s%8s%8s%s}',...       
                Color.pointDir.type,...
                Color.pointDir.paraPtr,...
                Color.pointDir.version,...
                Color.pointDir.lineStyle,...
                Color.pointDir.layer,...
                Color.pointDir.view,...
                Color.pointDir.matrix,...
                Color.pointDir.label,...
                Color.pointDir.status,...
                Color.pointDir.section,2*Index-1,...
                Color.pointDir.type1,...
                Color.pointDir.lineWeight,...
                Color.pointDir.color,...
                Color.pointDir.rowNum,...
                Color.pointDir.form,...
                Color.pointDir.reserved,...
                Color.pointDir.reserved1,...
                Color.pointDir.elementLabel,...
                Color.pointDir.elementIndex,...
                Color.pointDir.section1,2*Index);
        strPara_temp=sprintf('%s,%.1f,%.1f,%.1f;',...
                Color.pointDir.type,...
                Color.r(i),...
                Color.g(i),...
                Color.b(i));
        strPara_temp=sprintf('%-64s�P%7s',strPara_temp,2*Index-1,Color.pointDir.paraPtr);
       
        strDir=strcat(strDir,strDir_temp,'\n');
        strPara=strcat(strPara,strPara_temp,'\n');
    end
end

 

以上一共6个文件,调用关系如下

image

最后直接运行main文件即可,当命令框提示如下信息以后,表示产生文件结束:

正在写文件。。。
写文件结束。

 

然后可以将导入的iges文件导入其他CAD软件,另外可以先用一个小软件RegalIgs.exe测试一下产生的效果,如下

打开软件

image

代开iges文件

image

显示

image

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有