DXF tester for CNC cutting. Direct interaction with DXF without libraries (VBA)

Programming and macros
User avatar
mihkov
Posts: 44
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 17
x 24

DXF tester for CNC cutting. Direct interaction with DXF without libraries (VBA)

Unread post by mihkov »

The task was to make a simple tester of DXF files after Solidworks for laser cutting. It is necessary to check obvious errors: Intersection of contours, overlapping of contours, free end of contour (openness). The macro was not originally intended as a separate one, it was intended as a set of modules included in a larger macro for creating flat patterns. Therefore, there are 3 main modules:
1. DXF_tester_A_MAIN with the main function MainDxfTester that gets the full path to one Dxf file and returns an error code with the processing results:

Code: Select all

'Response:
'-1 Error not processed at all
'0 Unexpected error
'1 Processed no errors
'2 Processed there are errors Response save
'3 Processed there are errors Response do not save
2. DXF_tester_B_Reader with the main function ReadDxfFileAndFindInterSection that gets the full path to one Dxf file and reads geometric primitives from it into the Dictionary DIC_ReadDxfFile - this dictionary stores all information about layers - geometric objects on these layers - Errors on these layers (intersections, overlaps, etc.)
Since this was done for a specific production, the layers are set explicitly:

Code: Select all

"0" - cutting geometry
"UP" - bend lines up
"DOWN" - bend lines down
"BOX" - overall frames
"GRAV" - layer with engraving.
The function checks all layers for all errors. But of course it makes sense for the most important "0".
3. DXF_tester_C_Draw with the main function DrawDxf which draws geometry from DXF on the form, it should return True if everything is drawn well, but this is not done - there is no point. When searching for errors, the tolerance is used: Public Const Tolerance As Double = 0.005 in DXF file units (usually mm).

The main dictionary where all information is recorded is a dictionary with dictionaries, such a structure:
Dictionary("Layer Name")("Group Name")("Object Name")(array or collection with coordinates)
"Group Name" - "LINES", "ARCS", "CIRCLES", "POLYLINES", "SPLINES" (The macro works only with such geometric primitives)
"Object Name" - for example: "Spline2"
"LINES" - array Array(StartX, StartY, EndX, EndY)
"ARCS" - array Array(CenterX, CenterY, Radius, startAngle, endAngle)
"CIRCLES" - Array(CenterX, CenterY, Radius)
"POLYLINES", "SPLINES" - collection with arrays: Array(StartX, StartY)
For example:
DIC_ReadDxfFile.Item("0")("SPLINES")("Spline2")(1)(0)
1-first array in collection / 0-coordinate X

Form with output of graphic part: DXFtest_Form. Image from DXF is output within Label1 field. No scaling and manipulation with image. You can only look (for VBA and for this we should say thanks).
You can see total number of errors, number of each type of error, and also layer name with number of geometric objects on them and number of errors on each. Display of layers can be turned on and off. Display of errors can be turned on and off. Since the form is displayed in context of batch saving of DXF files, there is a timer for 10 seconds. User sees image with error and can make a decision: Save anyway (that is there is no forced error). Or can press Mark as Error which will happen automatically after 10 seconds.

The macro gave me the idea that I need to tie up with VBA and study VB.NET, and develop. VBA is reliable - but it is tied hand and foot and many ideas and their implementation simply have to be sacrificed. The macro is provided as is, because I have not found a single solution for processing the contents of DXV without connecting third-party libraries on the Internet.
Oh, yes. An important point to test all this, the launch model has been added: ListDxfTester in it Sub MainLisDXF - Excel is used here to select several DXF on the computer and conduct testing. (don't forget: Tools - Reverences - Excel (you can look here:
mihkov wrote: Wed May 22, 2024 2:56 am Option to select a folder in a convenient window using the Excel API in Solidworks VBA.
Dxf_tester_DXFtest_Form1.png
Red circle - intersection error
Yellow circle - free end error
Yellow circle with red dot - common end point for more than 2 geometric objects.
Blue line with dot in the middle - overlapping objects (common area shown)
**Below is a button to stop the timer "чекаю"
Attachments
DXF_TESTEReng.swp
(570.5 KiB) Downloaded 41 times
Dxf_tester_DXFtest_Form2.png
Post Reply