Close

Using ezx display with dxfout.h

A project log for A Simple DXF Output Library

Here is a simple DXF output library that may or may not be supported by your CAD package.

agpcooperagp.cooper 04/26/2019 at 09:070 Comments

Graphical Display Libraries

I used to use one of the "BGI" graphics libraries. A port of the old Borland BGI graphics library. I had a version for Linux and Windows. It's okay but I migrated to "ezxdisp" (http://morihit.net/ezxdisp/) because it was faster and had some useful features. Neither of the libraries will display images.

However, many people still use Windows and porting back to windows and using BGI library is inconvenient. Initially I had a little trouble compiling the Win32 "ezxdisp" library but if you read the "ezxdisp" webpage they point you to how to do it. Install "msys" and use that. Had a little trouble with setting up "msys" but that may have been because my hard disk was almost full.

Anyway, the Win32 (MinGW) library has been compiled and uploaded to my files area.

Compiling an "ezxdisp" Application

The main thing is to install the header and library in your preferred location.

Either /usr/include and /usr/lib for Linux, or /MinGW/include and /MinGW/lib for Windows (assuming your using MinGW).

If you really want to you can just copy the files into you code working directory, but remember to tell the compiler and linker to look in the work directory.

Linker Settings

This is perhaps the most import thing to know. I usually put a comment to remind me in the source code.

For Linux the linker settings are:

For Win32 (MinGW) it is:

Here is a code snippet on using "ezxdisp":

#include <stdio.h>
#include "ezxdisp.h"

int main(int argc, char *argv[])
{
  ezx_t *e;

  e = ezx_init(200, 200, "no title");
  ezx_set_background(e, &ezx_white);
  ezx_fillcircle_2d(e, 100, 100, 40, &ezx_red);
  ezx_redraw(e);
  ezx_pushbutton(e, NULL, NULL);
  ezx_quit(e);

  return 0;  
}

Editing the DXFOut demo to show what it is writing to file.

// Include Libraries
#include <stdio.h>
#include <stdlib.h>

// Please check dxfout.h for function parameter definitions
#include "dxfout.h"

#include "ezxdisp.h"
// -lezx -lX11 -lm             for Linux
// -lmingw32 -lezx -mwindows   for windows (MinGW)

// EZX display structure
ezx_t *disp;

// EZX window size
int width=1200;
int height=700;

// Nominal display scale
double scale=10.0;

int main(int argc,char **argv) {
  printf("DXFOut V1.1 - Written by Alan Cooper (AlanCooper@StartMail.com)\n");

  // Open a new DXF file
  char fileName[128];
  sprintf(fileName,"DXFOutTest.dxf");
  FILE *F1=fopen(fileName,"w");

  // Initialise graphics
  disp=ezx_init(width,height,"Show DXF");
  ezx_set_background(disp,&ezx_black);

  // Create a small box:
  //   Use Shape (closed polyline)
  //   Set Colour 0
  //   Set Layer "Test"
  //   Note: Z coordinate is 0
  DXF_Begin(F1);                        // Required once
  DXF_ShapeBegin(F1,"Test",0);          // Required for each shape (closed polyline)
  DXF_ShapePoint(F1,10,30,0);
  DXF_ShapePoint(F1,30,30,0);
  ezx_line_2d(disp,width/2+scale*10,height/2-scale*30,width/2+scale*30,height/2-scale*30,&ezx_white,1);
  DXF_ShapePoint(F1,30,10,0);
  ezx_line_2d(disp,width/2+scale*30,height/2-scale*30,width/2+scale*30,height/2-scale*10,&ezx_white,1);
  DXF_ShapePoint(F1,10,10,0);
  ezx_line_2d(disp,width/2+scale*30,height/2-scale*10,width/2+scale*10,height/2-scale*10,&ezx_white,1);
  ezx_line_2d(disp,width/2+scale*10,height/2-scale*10,width/2+scale*10,height/2-scale*30,&ezx_white,1);
  DXF_ShapeEnd(F1);                     // Required for each shape (closed polyline)

  // Put a point in the middle
  DXF_Point(F1,"Test",1,20,20,0);
  ezx_point_2d(disp,width/2+scale*(20),height/2-scale*(20),&ezx_red);

  // Make point look like a cross-hair using lines
  DXF_Line(F1,"Test",2,22,20,0,25,20,0);
  ezx_line_2d(disp,width/2+scale*22,height/2-scale*20,width/2+scale*25,height/2-scale*20,&ezx_yellow,1);
  DXF_Line(F1,"Test",2,18,20,0,15,20,0);
  ezx_line_2d(disp,width/2+scale*18,height/2-scale*20,width/2+scale*15,height/2-scale*20,&ezx_yellow,1);
  DXF_Line(F1,"Test",2,20,22,0,20,25,0);
  ezx_line_2d(disp,width/2+scale*20,height/2-scale*22,width/2+scale*20,height/2-scale*25,&ezx_yellow,1);
  DXF_Line(F1,"Test",2,20,18,0,20,15,0);
  ezx_line_2d(disp,width/2+scale*20,height/2-scale*18,width/2+scale*20,height/2-scale*15,&ezx_yellow,1);

  // Put a circle around the cross-hair
  DXF_Circle(F1,"Test",3,20,20,0,5);
  ezx_circle_2d(disp,width/2+scale*20,height/2-scale*20,scale*5,&ezx_green,1);


  // And another box but using polyline (open polyline)
  DXF_PolylineBegin(F1,"Test",4);       // Required for each shape (closed polyline)
  DXF_PolylinePoint(F1, 9,31,0);
  DXF_PolylinePoint(F1,31,31,0);
  ezx_line_2d(disp,width/2+scale*9,height/2-scale*31,width/2+scale*31,height/2-scale*31,&ezx_cyan,1);
  DXF_PolylinePoint(F1,31, 9,0);
  ezx_line_2d(disp,width/2+scale*31,height/2-scale*31,width/2+scale*31,height/2-scale*9,&ezx_cyan,1);
  DXF_PolylinePoint(F1, 9, 9,0);
  ezx_line_2d(disp,width/2+scale*31,height/2-scale*9,width/2+scale*9,height/2-scale*9,&ezx_cyan,1);
    DXF_PolylinePoint(F1, 9,31,0); // Close the box
  ezx_line_2d(disp,width/2+scale*9,height/2-scale*9,width/2+scale*9,height/2-scale*31,&ezx_cyan,1);
  DXF_PolylineEnd(F1);                  // Required for each shape (closed polyline)

  DXF_End(F1);                          // Required once

  // Show the display and wait for exit
  ezx_redraw(disp);
  while (!ezx_isclosed(disp));
  ezx_quit(disp);

  // Close the DXF file
  fclose(F1);

  return 0;
}

So all I have done is add initilisation code to set up the graphics pointer (i.e. disp), mirrored the DXF code with graphics code, and after showing the code, waited until the window is killed. Although I set the graphics scale to 10.0, it is usually determined by testing the range of the data to be displayed.

AlanX

Discussions