dxf2

Upload: glodovichi

Post on 14-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 DXF2

    1/6

    DXF Notes

    Abstract:Notes for writing your own DXF output format. Covers flatheirachy-less output and also heirachical output using BLOCKS andINSERTS.

    Layout of a DXF File

    Overall layout---------------A text based DXF file usually consists of three sections,

    A Tables section, which contains only Autocad view and configurationrelevant information. This section is not compulsory.

    A Blocks section, which contains definitions of objects which may ormay not be used in this "drawing". This section is not compulsory.

    An Entities section, this contains the actual objects as they are toappear in the drawing. This section is most definitely used.

    GROUP codes etc.----------------

    A text based DXF file consists of pairs of lines, the first line of apair is an integer less than a thousand and identifies what the dataon the second line is used for.

    This format is fairly peculiar to DXF files and was probablyimplemented for a language that could interpret numbers easier thanit could interpret strings (e.g fortran), I'm only guessing here mind.

    Throughout this document to save paper/screen size I'll use the

    shorthand of first_line/second_line rather than put the code and thedata on separate lines. So If you see 0/ENDSEC you'll know the fileshould contain two lines with 0 on the first line, and ENDSEC on thesecond:

    Group code 0 is always used to announce a string, which is usually ofa command nature, for example 0/SECTION announces the start of asection, 0/EOF announces the end of the file, and so on.

    Structure Again---------------

    ALL DXF Files end with a 0/EOF code.

    Each of the three sections starts with a 0/SECTION and ends with a0/ENDSEC. After the 0/SECTION you get one of :

    2/ENTITIES, or 0/BLOCK, or ????/TABLES

    2/ENTITIES----------

  • 7/29/2019 DXF2

    2/6

    2/ENTITIES this starts the actual drawing data and is the lastsection in a DXF file, you can define your entire drawing in thissection.

    To output a polygon we output 0/3DFACE to announce that we have afour (or three) sided polygon 8/0 to indicate layer, 6/CONTINUOUS toindicate that this polygon could be smoothed, 62/2 to select colour 2and

    10/Xcoord, 20/Ycoord 30/ZCoord for first vertex11/Xcoord, 21/Ycoord 31/ZCoord for second vertex12/Xcoord, 22/Ycoord 32/ZCoord for third vertex13/Xcoord, 23/Ycoord 33/ZCoord for fourth vertex

    To output a triangle make the third and fourth vertex the same.

    so we have0/3DFACE8/06/CONTINUOUS62/210/XCoord20/YCoord

    30/ZCoord11/XCoord121/YCoord131/ZCoord112/XCoord222/YCoord232/ZCoord213/XCoord323/YCoord333/ZCoord3

    for a complete 3d face.

    Now we have output a 3DFace, we can then output more 3D faces in thesame way or end using 0/ENDSEC.

    If you are outputing a hierachy less file this is all you need.

    HIERACHY========

    0/BLOCKS--------

    0/BLOCKS starts the named objects section of the DXF file, thissection can be completly omitted, or it will come before the

    2/ENTITIES section. This section can be used to establish a heirachyof objects, or simply define some very frequently used objects.

    The data output in this section is the same as the data you canoutput in the 0/ENTITIES section, only here you can also name a groupof output primitives to form an object. To create a named object yououtput

    0/BLOCK2/block_name

  • 7/29/2019 DXF2

    3/6

    ...

    ... output your 3DFaces here

    ...0/ENDBLK

    you can repeat this sequence as often as you like.

    If you want to place an already named object inside another object,or to use it in the final 0/ENTITIES section xyou use a 0/INSERTsequence, like so:

    0/INSERT2/name name of object to insert here41/XScaling 42/YScaling 43/ZScaling Scaling of the object to be inserted210/Xcomponent220/YComponent230/ZComponent Components of the Z axis for the objectto be inserted50/Rotation Rotation about this Z axis10/X20/Y30/Z Translation along this new coordinate system

    An insert sequence is terminated by a new delimiter probably a 0/group such as another 0/INSERT or 0/BLOCK, or a 0/ENDBLK.

    Actually Coding all this.-------------------------

    I'll not give you much help on coding a simple DXF file whichconsists of only a simple 0/ENTITIES section with 3DFaces in it asthis is fairly trivial. But the reason I've written this document is

    to help you output using 0/BLOCKS and 0/INSERT since this took me fartoo long to figure out on my own and I couldn't find any simpleonline references to help me.

    Generating a 0/INSERT---------------------

    I will assume you have defined a unit box using 0/BLOCK, 2/MyBox, andsix 0/3DFACE's, this box is centered on the origin. You now want toplace a box at some other location at some arbitrary rotation andsome arbitrary scaling.

    Here is a code fragment of how I do this. I have a TransformationMatrix representing the orientation, positioning and scaling (I onlyallow uniform scaling and no shear).

    In the code fragments any output is shown with a > at the start of the line

    float ang;Vector NewZ;

  • 7/29/2019 DXF2

    4/6

    Vector NewX;Vector NewY;

    // output an insert section>0/INSERT>2/MyBox

    // output the scaling>41/scale (x)>42/scale (y)>43/scale (z)

    // orient the object's Z axis// this always passes through the origin// got by multiplying the unit z axis by the rotation portion of

    // the transformation matrixNewZ = Vector(0.0f,0.0f,1.0f) * TF.RotationOnly();

    // and output this// new Z axis

    >210/NewZ.x>220/NewZ.y

    >230/NewZ.z

    // Now we have to decide how the X axis is aligned// this was derived painstakingly from autocads description// of the arbitrary axis theorem

    if (fabs(NewZ.x) < 1.0/64.0 && fabs(NewZ.y) < 1.0/64.0){// if the NEW Z axis lies close to the Y axis then ....

    NewX = Vector(0.0f,1.0f,0.0f); // X axisNewX = NewX NewZ; // cross

    ed with the NEW Z axis NewX = NewX / NewX.mag(); // and renormalised

    }else

    {float ang;NewX = Vector(0.0f,0.0f,1.0f); // Y axisNewX = NewX NewZ; // crossed with

    the NEW Z axisNewX = NewX / NewX.mag();}

    // and calculate the NewY axis.NewY = NewZ NewX; // cross the Z and X axe

    sNewY = NewY / NewY.mag(); // renormalise

    // having got this X axis and Z axis we will need to rotate// around the NEW Z axis to re-orient our personal Y axis.

    // Rotation about this new Z axis to re-align the X axis?

  • 7/29/2019 DXF2

    5/6

    Vector XAxis1 = Vector(1,0,0) * TF.RotationOnly();

    float dotprod = XAxis1 % NewX; // take the dot product

    if (dotprod > 1.0){dotprod = 1.0; // correct for any rounding errors}

    else if (dotprod < -1.0){dotprod = -1.0; // correct for any rounding errors error

    !}

    ang = acos(dotprod); // dtprod = 1 when axes are alignedang *= 180.0f/M_PI;

    // how do we decide if angle is > 180!// if cross prod is along NEWZ?Vector AnotherZ = NewX ^ XAxis1;if (AnotherZ % NewZ < 0)

    {ang = 360 - ang;

    }

    // now output this rotation

    >50/ang

    // Build a transformation matrix representing this new coordinate

    // system// such that this transformation matrix should place ..

    TransformationMatrix NewCoordSystem;

    NewCoordSystem.matrix[0] = NewX;NewCoordSystem.matrix[1] = NewY;NewCoordSystem.matrix[2] = NewZ;NewCoordSystem.matrix[3] = Vector(0,0,0); // goes though o

    rigin

    // so no translationNewCoordSystem.IsIdentity = 0;

    // now the transforms, remembering that these are going to be relative

    // to the new Z axis!

    // so we must use the inverse of the new Matrix!TransformationMatrix InvertOfNewCoordSystem;

    // because this is a simple orthognal? matrix all we ahve to do// is take the transform of it!

  • 7/29/2019 DXF2

    6/6

    InvertOfNewCoordSystem.matrix[0] = Vector(NewCoordSystem.matrix[0].x,

    NewCoordSystem.matrix[1].x,NewCoordSystem.matrix[2].x);

    InvertOfNewCoordSystem.matrix[1] = Vector(NewCoordSystem.matrix[0].y,

    NewCoordSystem.matrix[1].y,NewCoordSystem.matrix[2].y);

    InvertOfNewCoordSystem.matrix[2] = Vector(NewCoordSystem.matrix[0].z,

    NewCoordSystem.matrix[1].z,NewCoordSystem.matrix[2].z);

    InvertOfNewCoordSystem.matrix[3] = Vector(0,0,0);// still no Tran

    slationInvertOfNewCoordSystem.IsIdentity = 0;

    Vector Translat = TF.Translation();

    Translat = Translat * InvertOfNewCoordSystem;

    // dxf_xyz(f,Translat);>10/>20/>30/

    }}