Brian,
Exporting DXF from Visual Basic:
The hard part is finding the documentation, and reading through the
messy data formats. When you've done all that - it turns out its very
simple to export a minimal dxf from basic, JAVA, or even an Excel
spreadsheet with a VB Macro (simple also). By minimal, I do NOT mean
small, it can have lots of data! I haven't found much use for HPGL (HP
Plotter Graphics Language) since 1980 or so - back in those days I
generated trochoids for Rotary piston compressor drawings (think
inverted Wankel) on an Apple and exported the HPGL as a text file to -
what else - yup - an HP Plotter. Do you still need HPGL today? - I'd
wonder why you do?
I have attached a .bas file (renamed with .txt extension to sneak past
antivirus apps) showing how I export dxf. Here are some pointers:
0) Look at the function called "Function DXF_Out_Test()" - it is a
complete example on its own.
1) I no longer use visual basic - all my new work is JAVA - its just so
much cleaner. This code translates well into JAVA. JAVA IDE's are
excellent and free and many, JAVA is fully Object Oriented - very nice -
I could go on......
2) dxf is really just a special label on a human readable text file, so
output routines are easy to write. They just output text files, and set
the extension to ".dxf". A really good way to learn dxf (also the hard
way - that's a minor detail) is to export dxf from a CAD package, then
make one minor change using your CAD program, and export it a second
time. Now change the file extensions for both from dxf to txt. Now
compare them in a text editor that allows changes between to file to be
detected - like Word. Your minor change shows up as the difference in
the text files (Word underlines the changes in Red I believe). You will
very quickly see how basic shapes and fonts, etc are formatted. Lots of
coffee required.
3) My code shows examples of 2D primitives, like lines, arcs, circles,
ellipses. But there is a third coordinate in the format for Z, so
extending to 3D is trivial. Here, 3D still means lines and curves in
space, not surfaces.
4) My general approach to coding is to create all the coordinates in my
sundial number crunching routines, and store it in a (huge) array as I
calculate it. Then when done crunching, I pass the array (now called a
"display list") of coordinates to the dxf output routines. I found that
Visual Basic 6 had all sorts of array size limits and memory problems
forcing me to declare some variables as global when they should have had
local scope, JAVA was essentially unlimited here. Perhaps the MS .NET
stuff is better, but I'm a JAVA convert now - its too late for me to
mess with V'Basic! I also have moved beyond dxf (that shall remain a
hint for now) as a preferred output format. Of course - it depends on
what you wish to do with it. Many sundial routines will not need huge
arrays - I was doing some goofy stuff, and needed many little line segments.
5) These routines work well with Autocad, Solidworks, etc - all import
it easily. One exception is that AutoCad Lite (or LT ??) does not
handle ellipses as a curve, but rather a series of line segments, so it
doesn't import that very well. Just avoid ellipses - use lots of line
segments instead. But I have not used autocad for years - so maybe it
has changed. The number one issue with importing this has to do with
scaling - you often have to define units, or re-scale after importing,
depending on what CAD or CAM system is importing it.
6) While I reverse engineered this stuff the first time I did dxf output
back in 1989 using the hard way noted above, I have since realized that
most of the dxf content in a typical dxf file from a commercial program
is baggage (or more politely - they are format enhancements for features
you are unlikely to ever use). Paul Bourke - has some great info on
dxf. See his URL:
http://local.wasp.uwa.edu.au/~pbourke/dataformats/dxf/min3d.html You
can also get dxf info from Autocad website - you'll need to hunt a bit -
its there. AC considers their dwg format to be proprietary, but they
publish the dxf spec. If you climb up Bourke's URL, you'll also find
he has a wealth of geometrical algorithms, some perhaps very useful for
programatically generating 2D graphics for sundials. I'd like to meet
him someday!
Special Note
While none of this is really very secret - I did put a lot of work into
this over the years, and would be very grateful if those who find this
information useful would reciprocate by sending me their code, or their
completed sundial programs to me (binary or executables of commercial
result OK). Maybe someone will add it as a Macro to an Excel sheet for
dials? Please do not send anything you intend to patent until you've
filed the application with the patent office - I don't want any
conflicts of interest. (But sundials are so much fun - do you really
want to file a patent?) I will not redistribute. My interest is in not
having to pay for sundial code that I might want to use - especially if
I helped in some way!
Comments?
Let me know if you found this helpful,
Thanks,
- Ben Hoffmann - Minneapolis, MN
Brian Albinson wrote:
Hi Folks
Can anyone steer me in the right direction for the method of producing
.dxf or .hpgl format files directly from Visual Basic ?
Brian Albinson
Vancouver, Canada
---------------------------------------------------
https://lists.uni-koeln.de/mailman/listinfo/sundial
Attribute VB_Name = "Module4"
'
' Module - Contains DXF Output Code and Display List Code
*********************************************************************
*********************************************************************
Update added March 21, 2007:
!!!!!!!!!! PLEASE READ !!!!!!!!!
While all this looks pretty simple, it did take a lot of work to figure it
out the first time through. Thus I request the following,
if this helps you with your project, please send me source code
for the part of your project related to dxf routines.
Also, this was done for sundial applications, so if you use this for a sundial
application, please send me a free copy so I may see what you did.
These terms are "management by guilt" so please be considerate!
Thanks!
- Ben Hoffmann
[EMAIL PROTECTED]
************************************************************************
************************************************************************
'
' Status:
' 5.31.2002 appears to work
'
' Code for General Purpose Display List creation and Export of List to DXF Files
' Taken from code used in SD project - but updated for new approach.
'
' **************************************************************************
' Note that this code, including Type Def's, will replace existing stuff
' **************************************************************************
'
' this is in general meant for 2D data - but may (depending on useage) also
' work for 3D dxf display lists
'
' NB - start all related routines with the name "DisplayList_Something"
' *****************************************************************************
' routines needed:
'
' STARTED => Simple Add - which is appln specific - but will add an item to
display list
' using minimal paramters (others are defaulted or zerod out)
' idea is to minimaize the number of paramters to set before calling routine.
' STARTED => output a display list item in dxf format (part of one large dxf
out routine,
' with Select Case sections for each entity type
' 2D array of sequential points => line segments in Display List
' This is a common situation, and could be done on the fly, but that
' would mean extra code, so at least work up a code template,
' if not a seperate routine to do this.
' (issue has to do with passing arrays - so code template makes sense)
' (by template I mean a chunk of code that gets cut and pasted often)
'
' future - display list manipulation routines (many possible)
' sort by something (layer, feature, color) and make a new list
' examine list for max size (bouning box or circle)
' Complicated Add - add display list item to global display list, with
control over
' all parameter values
'
' More Sophisticated:
' clip entire list to some limit - like a line, ray, bounding box, circle,
etc
' rotate / translate list
' merge two lists
'
' Needed for sure ****************************************************
' Routine to dynamically resize array
' Routine or place to initialize gMDL and the Tally parameter (set to Zero)
'
' *****************************************************************************
Public gMDL(30000) As DisplayListItem ' use as Master Display List
' use index = 0 to store some list info if needed, first entity is at index
= 1
Public gMDL_Current_Tally As Integer ' use as upper limit of array for gMDL
Public gFileOutName As String
Function DXF_Out_Test()
'
' started on 5.21.2002
'
' **************************
' Save for Posterity
' contains code examples
' to be used in the future
' **************************
'
' derived from HTML file provided by AutoCad for writing dxf code
' look at page 250 and page 745 of VB6 Programmer's Guide
' to get examples of GUI for
' examples of GUI's to get file names.
' need to put common dialog control on form - or menu,
' do a button for now - change to a menu item later
' then pressing this buttong launches a routine like that
' on page 250, which in turn calls this routine
' (menu more complicated - as it needs to know when it can be valid choice)
OutFileName = "Test_File_Name.dxf"
' puts test file in same folder as this test code
Open OutFileName For Output As 1
' Write Out Minimal Header Info %%%%%%%%%%%%%%%%%%
Print #1, 0
Print #1, "SECTION"
Print #1, 2
Print #1, "ENTITIES"
' Write out a Line Segment %%%%%%%%%%%%%%%%%%%%%%%%
x1 = 1
x2 = 2
y1 = 1
y2 = 2
z1 = 0
z2 = 0
Print #1, 0 ' Entity Flag"
Print #1, "LINE"
Print #1, 8 ' Layer Flag
Print #1, "0" 'Layer Number
Print #1, 62 ' Color Flag
Print #1, "1" ' Color Value
Print #1, 10 ' Line Segment - First End Point
Print #1, x1
Print #1, 20
Print #1, y1
Print #1, 30
Print #1, z1
Print #1, 11 ' Line Segment - Second End Point
Print #1, x2
Print #1, 21
Print #1, y2
Print #1, 31
Print #1, z2
' Write out a Circle %%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1 = 1
y1 = 1
z1 = 0
R1 = 1
Print #1, 0 ' Entity Flag
Print #1, "CIRCLE"
Print #1, 8 ' Layer Flag
Print #1, "0" ' Layer Number
Print #1, 62 ' Color Flag
Print #1, "5" ' Color Value
Print #1, 10 ' Circle Center
Print #1, x1
Print #1, 20
Print #1, y1
Print #1, 30
Print #1, z1
Print #1, 40 ' Circle Radius
Print #1, R1
' Write out an Arc %%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1 = 1
y1 = 1
z1 = 0
R1 = 2
S1 = 0 ' Start Angle Units Degrees
S2 = 90 ' End Angle
' Notes regarding Angle Limits]
' 0 is Plus X axis
' Angles are RH Rule
' if S2 = 360 - all you get is a dot on the Plus X axis.
' Angles appear to wrap - (0,540) same result as (0,180)
' (180,0) goes from 180 to 360 (ie - half circle below X axis)
' (180,20) goes from 180 in RH rule direction to +20
' Conclusion on Arc Angle Limit Rules:
' Angle are absolute wrt std 0,360 circle and wrap as expected
' (so 180 same as -180)
' Arc starts at first angle and draws in RH rule direction
' (no matter what the second limit is) until it gets to the second
limit.
' And - you can't use an arc as a default circle because 0,360 fails to
' plot a circle (just a pt).
Print #1, 0 ' Entity Flag
Print #1, "ARC"
Print #1, 8 ' Layer Flag
Print #1, "0" ' Layer Number
Print #1, 62 ' Color Flag
Print #1, "5" ' Color Value
Print #1, 10 ' Arc Center
Print #1, x1
Print #1, 20
Print #1, y1
Print #1, 30
Print #1, z1
Print #1, 40 ' Arc Radius
Print #1, R1
Print #1, 50 ' Start Angle
Print #1, S1
Print #1, 51 ' End Angle
Print #1, S2
' Write out an Ellipse %%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1 = 1
y1 = 1
z1 = 0
x2 = 1
y2 = 1
z2 = 0
R1 = 2
S1 = 0 ' Start Parameter Units = Radians
S2 = 3.14159 ' End Parameter
' These units and limit rules are the same as for circles, EXCEPT
' units are radians and not Degrees,
' AND angle parameter is wrt Vector 2 (x2,y2,z2), not + x axis.
'
' Note that S2 = 2* 3.14159 gives almost a full ellipse,
' but that a pinch more and it wraps so that you get a very small
' fraction of an ellipse.
Print #1, 0 ' Entity Flag
Print #1, "ELLIPSE"
Print #1, 8 ' Layer Flag
Print #1, "0" ' Layer Number
Print #1, 62 ' Color Flag
Print #1, "6" ' Color Value
Print #1, 10 ' Ellipse Center
Print #1, x1
Print #1, 20
Print #1, y1
Print #1, 30
Print #1, z1
Print #1, 11 ' Ellipse End Pt of Major Axis, Relative to Center
Print #1, x2
Print #1, 21
Print #1, y2
Print #1, 31
Print #1, z2
Print #1, 40 ' Ellipse - Ratio of Minor/Major Axis
Print #1, R1
Print #1, 41 ' Start Parameter - Radians, relative to Major Axis
Print #1, S1
Print #1, 42 ' End Parameter - Radians, CCW direction
Print #1, S2
' Close up Entity Section %%%%%%%%%%%%%%%%%%%%%%%%
Print #1, 0
Print #1, "ENDSEC"
' Close up File Flags %%%%%%%%%%%%%%%%%%%%%%%%%%%%
Print #1, 0
Print #1, "EOF"
Close 1
End Function
Function DisplayList_DXF_Out()
'
' Date = 5.31.2002
'
' outputs the global display list = gMDL to DXF file on hard disk
Dim Entity_Num As Integer, Color_Local As Integer
Dim x1 As Double, y1 As Double, z1 As Double
Dim x2 As Double, y2 As Double, z2 As Double
Dim R1 As Double
'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' %%%%%%%%%%%%%%%% Open Output File and write initial header
%%%%%%%%%%%%%%%%%
'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Open gFileOutName For Output As 1
' Write Out Minimal Header Info
Print #1, 0
Print #1, "SECTION"
Print #1, 2
Print #1, "ENTITIES"
' Main Loop
For Index = 1 To gMDL_Current_Tally
Entity_Num = gMDL(Index).Entity_Type
Select Case Entity_Num
Case 1 ' Line Segment
' Load correct data for Line Segment
x1 = gMDL(Index).P1
y1 = gMDL(Index).P2
z1 = gMDL(Index).P3
x2 = gMDL(Index).P4
y2 = gMDL(Index).P5
z2 = gMDL(Index).P6
Color_Local = gMDL(Index).Color_Num
' Write out a Line Segment
Print #1, 0 ' Entity Flag"
Print #1, "LINE"
Print #1, 8 ' Layer Flag
Print #1, "0" 'Layer Number ' just a default for now
Print #1, 62 ' Color Flag
Print #1, Color_Local ' Color Value - see if Integer type works
**************
' otherwise implement string like "1" as before
Print #1, 10 ' Line Segment - First End Point
Print #1, x1
Print #1, 20
Print #1, y1
Print #1, 30
Print #1, z1
Print #1, 11 ' Line Segment - Second End Point
Print #1, x2
Print #1, 21
Print #1, y2
Print #1, 31
Print #1, z2
Case 2 ' Circle
' Load correct data for Circle
x1 = gMDL(Index).P1
y1 = gMDL(Index).P2
z1 = gMDL(Index).P3
R1 = gMDL(Index).P4
Color_Local = gMDL(Index).Color_Num
' Write out a Circle
Print #1, 0 ' Entity Flag
Print #1, "CIRCLE"
Print #1, 8 ' Layer Flag
Print #1, "0" ' Layer Number
Print #1, 62 ' Color Flag
Print #1, Color_Local ' Color Value - see if Integer type works
**************
' otherwise implement string like "1" as before
Print #1, 10 ' Circle Center
Print #1, x1
Print #1, 20
Print #1, y1
Print #1, 30
Print #1, z1
Print #1, 40 ' Circle Radius
Print #1, R1
Case 3 ' Arc - not yet supportted
************************************
Stop
Case 4 ' Ellipse - not yet supported
*********************************
Stop
Case Else ' error = unsupported entity type
Stop
End Select
Next Index
'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' %%%%%%%%%%%%%%%% Close Output File
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Close up Entity Section %%%%%%%%%%%%%%%%%%%%%%%%
Print #1, 0
Print #1, "ENDSEC"
' Close up File Flags %%%%%%%%%%%%%%%%%%%%%%%%%%%%
Print #1, 0
Print #1, "EOF"
Close 1
End Function
Function Do_Next_Read_Me()
'
' Do Nothing Function to track what to do next
'
' Status 5.22.2002.8:30pm
' Code works, writes test file to local dir
' just run in IDE and click on Form One Button
' to execute the function that writes a test
' dxf file.
'
' Next
'
' Color:
' Here are the colors on AC Lite (appears the same in SolidWorks & Design
CAD):
' Results were on a white background - may change on black background
' 0 = black
' 1 = red
' 2 = yellow (avoid yellow - very hard to see on white background)
' 3 = green
' 4 = cyan
' 5 = blue
' 6 = magenta
' 7 = black (again)
' 8 = lt black or gray
' 9 = very lt gray (white if background is black in Solidworks)
' Plan on using 1 thru 6 only - will work on white or black background
' if using white background - use 0 for black lines
' if using black background - use 9 for white lines
' I'll use 0 thru 6 for white backgrounds.
' Best Colors = 0,1,5,6
'
' What Works so far:
' Lines, circles, arcs and elliptical arcs work
' need to try on various CAD systems
'
' NEXT TO DO:
' and try to add layers
'
' NOTES FOR FUTURE WORK:
' DXF Entity Types (notes as follows):
' LW PLINE = Lightweight Polyline in R14 and above
' Its a 2d only version of PLINE - looks easier to code I think
' PLINE = Polyline - this is the geneeral purpose polyline
' Don't really understand the documentation on this one.
' MLINE = multiline - parallel lines - don't bother with this.
' SPLINE = spline - would like to - but don't undertsand documentation yet.
' XLINE = construction line - which is a line with no endpoints
' RAY = a construction line with one endpoint
' NOTE => XLINE's & RAY's are normally put on their own LAYER.
' POINT = point - easy to implement - may be useful.\
' some CAD programs (liek AutoCad) have special styles for Points.
'
' Future stuff to consider first would be POINTS, XLINE, RAY
End Function
Function DisplayList_Add_Manyline(ML_Data As ManyLine2D)
'
' Date of origination 7.28.2002
' WIP
**********************************************************************************
' Status Note 7.28.2002 - needs complete re-write - this was wholesale
copied **********
'
' Future ******************
' Use the "slow" version of Add Line Segment to get more paramters installed
' into the display list (like layers) - this also means going back to
extend the
' input data Type Defn's and subroutines that generate the data.
' Also add a sundial entity to the Display List for future filtering.
' *************************
'
' Input is a ManyLine2D data type (array of points to be turned into line
segments)
' Output is display list line segments
'
' Routine is similar to the one that adds a multiline to a display list.
' A ManyLine2D is a set of seperate and distinct lines, related in some way,
' a good example is shadow lines.
Dim NumPts As Integer, tempcolor As Integer
Dim x1 As Double, y1 As Double, z1 As Double, x2 As Double, y2 As Double,
z2 As Double
NumPts = ML_Data.Count
tempcolor = ML_Data.Color
For i = 1 To NumPts
x1 = ML_Data.Segment(i).PointStart.X
y1 = ML_Data.Segment(i).PointStart.Y
z1 = 0
x2 = ML_Data.Segment(i).PointEnd.X
y2 = ML_Data.Segment(i).PointEnd.Y
z2 = 0
Call DisplayList_AddSlow_LineSegment(x1, y1, z1, x2, y2, z2, tempcolor)
Next i
End Function
Function DisplayList_Add_Multiline(M_Data As MultiLine2D)
'
' Date of origination 7.13.2002
' WIP *****************************************
'
' Future ******************
' Use the "slow" version of Add Line Segment to get more paramters installed
' into the display list (like layers) - this also means going back to
extend the
' input data Type Defn's and subroutines that generate the data.
' Also add a sundial entity to the Display List for future filtering.
' *************************
'
' Iput is a multiline data type (array of points to be turned into line
segments)
' Output is display list line segments
Dim NumPts As Integer
Dim x1 As Double, y1 As Double, z1 As Double
Dim x2 As Double, y2 As Double, z2 As Double
Dim ColInt As Integer, Flag_as_ClosedCurve As Integer
' set intial paramters
NumPts = M_Data.Upperindex
ColInt = M_Data.Color
Flag_as_ClosedCurve = M_Data.Closed
For j = 1 To (NumPts - 1)
x1 = M_Data.X(j)
x2 = M_Data.X(j + 1)
y1 = M_Data.Y(j)
y2 = M_Data.Y(j + 1)
z1 = 0: z2 = 0
Call DisplayList_AddFast_LineSegment(x1, y1, z1, x2, y2, z2, ColInt)
Next j
If (Flag_as_ClosedCurve = 1) Then
' code block
x1 = M_Data.X(NumPts)
x2 = M_Data.X(1)
y1 = M_Data.Y(NumPts)
y2 = M_Data.Y(1)
z1 = 0: z2 = 0
Call DisplayList_AddFast_LineSegment(x1, y1, z1, x2, y2, z2, ColInt)
End If
End Function
Function DisplayList_AddFast_LineSegment(Xpt1 As Double, Ypt1 As Double, _
Zpt1 As Double, Xpt2 As Double, Ypt2 As Double, Zpt2 As Double, _
ColorNum As Integer)
'
' this is a simple routine for adding line sgements
' "Fast" in the name means that all you do is provide the endpoints
' and the color, and it will assign (or ignore) all the other parameters.
Dim i As Integer
' call routine to resize array if needed
*************************************
gMDL_Current_Tally = gMDL_Current_Tally + 1
i = gMDL_Current_Tally
gMDL(i).Entity_Type = 1
gMDL(i).P1 = Xpt1
gMDL(i).P2 = Ypt1
gMDL(i).P3 = Zpt1
gMDL(i).P4 = Xpt2
gMDL(i).P5 = Ypt2
gMDL(i).P6 = Zpt2
gMDL(i).Color_Num = ColorNum
End Function
Function DisplayList_AddSlow_LineSegment(Xpt1 As Double, Ypt1 As Double, _
Zpt1 As Double, Xpt2 As Double, Ypt2 As Double, Zpt2 As Double, _
ColorNum As Integer)
'
'
' Note as of 7.13.2002 - this is same as the "fast" version - waiting for
' future code to add more detail
'
' ***************************************************************
' add layer info, more color control, etc.
' ***************************************************************
'
' same as DisplayList_AddFast_LineSegment, excpet that we have
' more detail, so I call it ther "slow" routine
'
' this is a routine for adding line sgements
' "Slow" in the name means that you do is provide all the data needed
' and the color, and it will assign (or ignore) all the other parameters.
Dim i As Integer
' call routine to resize array if needed
*************************************
gMDL_Current_Tally = gMDL_Current_Tally + 1
i = gMDL_Current_Tally
gMDL(i).Entity_Type = 1
gMDL(i).P1 = Xpt1
gMDL(i).P2 = Ypt1
gMDL(i).P3 = Zpt1
gMDL(i).P4 = Xpt2
gMDL(i).P5 = Ypt2
gMDL(i).P6 = Zpt2
gMDL(i).Color_Num = ColorNum
End Function
Function DisplayList_AddFast_Circle(Xpt1 As Double, Ypt1 As Double, _
Zpt1 As Double, RadiusCirc As Double, ColorNum As Integer)
'
' this is a simple routine for adding line sgements
' "Fast" in the name means that all you do is provide the endpoints
' and the color, and it will assign (or ignore) all the other parameters.
Dim i As Integer
' call routine to resize array if needed
*************************************
gMDL_Current_Tally = gMDL_Current_Tally + 1
i = gMDL_Current_Tally
gMDL(i).Entity_Type = 2
gMDL(i).P1 = Xpt1
gMDL(i).P2 = Ypt1
gMDL(i).P3 = Zpt1
gMDL(i).P4 = RadiusCirc
gMDL(i).Color_Num = ColorNum
End Function
---------------------------------------------------
https://lists.uni-koeln.de/mailman/listinfo/sundial