Extract unit of measurement from multiple CAD files

Extract unit of measurement from multiple CAD files

marius.wolke
Participant Participant
1,872 Views
12 Replies
Message 1 of 13

Extract unit of measurement from multiple CAD files

marius.wolke
Participant
Participant

Hi everybody,

 

I have a customer that needs to upload 4500+ CAD files to a database where it is necessary to know the unit of measurement (Meters, Millimeters,, Inches, Feet, etc.) for every CAD file that is used. Is there an easy way to do this and to wright it to a (txt, csv, excel, etc.) file instead of opening every CAD file separately?

 

Thnx!

0 Likes
1,873 Views
12 Replies
Replies (12)
Message 2 of 13

daniel_cadext
Advisor
Advisor

Better to use another language where you can open side databases

here's a sample in python.. .NET is also a good choice

 

 

import traceback
from pyrx_imp import Rx, Ge, Db, Ap, Ed, Gi

def measurementValue(path) -> Db.MeasurementValue:
    sideDb = Db.Database(False, True)
    sideDb.readDwgFile(path)
    return sideDb.measurement()

def PyRxCmd_doit():
    try:    
        paths= ["E:/Test.dwg"]
        for path in paths:
            print(measurementValue(path))#Write to file
    except Exception as err:
        traceback.print_exception(err)
        

 

 

Command: DOIT
kMetric

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 3 of 13

marius.wolke
Participant
Participant

Hi Daniel,

 

Thnx for your reply, but the files aren't in the database yet.

We need to know first wat the measurement is before we can import/upload the CAD files to the database. 

0 Likes
Message 4 of 13

MrJSmith
Advocate
Advocate

I assume you are referring to the insertion units? You can use (getvar "INSUNITS") to return the insertion scale.

 

The problem you can run into is it isn't uncommon for someone to draw (or export from another program) a CAD file that is drawn with length decimal where each unit 1 refers to feet but they (or the program) did not set an insertion scale. The only way to figure out what scale they drew at is measuring a known length of an object.

 

https://www.landfx.com/docs/cad-basics/autocad-getting-started/4834-units.html#drawing_units

0 Likes
Message 5 of 13

paullimapa
Mentor
Mentor

As @MrJSmith replied and you also received similar replies from your OP there’s no way to get an accurate reading unless you manually open the dwg and measure a familiar geometry to validate what unit is assumed by the author. So I assume your purpose is to not just get whatever the lunits & insunits are currently set


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 6 of 13

daniel_cadext
Advisor
Advisor

"but the files aren't in the database yet."

 

Correct, the routine I posted will open the drawing (which is also considered a database) and read the data you want

You would need to feed the routine the paths to the drawing files you wish to check

 

 

 

import traceback
from pyrx_imp import Rx, Ge, Db, Ap, Ed, Gi
import openpyxl as xl

def measurementValue(path):
    sideDb = Db.Database(False, True)
    sideDb.readDwgFile(path)
    return [
        str(sideDb.lunits()),
        str(sideDb.luprec()),
        str(sideDb.insunits()),
        str(sideDb.measurement()),
    ]

def PyRxCmd_doit():
    try:
        wb = xl.Workbook()
        ws = wb.active
        paths = ["E:/Test.dwg", "E:/everyline.dwg", "E:/06457Submittal.dwg"]
        for path in paths:
            data = [path]
            data.extend(measurementValue(path))
            ws.append(data)
        wb.save("E:/woohoo.xlsx")

    except Exception as err:
        traceback.print_exception(err)

 

 

 

xl.png

 

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 7 of 13

daniel_cadext
Advisor
Advisor

Just to address the other posters concerns, sideDb is the dwg drawing.

From there you should be able to validate by scanning the entities in the drawing,

I.e. Dimension, text and table styles or entities.

 

Python might not be right for you;

Consider hiring programmer, maybe, someone in the. ARX or .NET forums. familiar with readDwgFile

 

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 8 of 13

Sea-Haven
Mentor
Mentor

As mentioned looking at Lunits variable could be done with OBDX which does not graphically open a dwg but looks inside it. Second suggestion like this is use the inbuilt Accoreconsole which is extremely fast doing tasks like this.

 

Ok now for file result that is pretty easy you use (setq fo (open (setq fname "yourfile") "A")) the A is append option so adds to a file. One issue I am thinking about is 4500 files the automation has to open and close the file 4500 times. So doing say 100 dwgs at a time would be best.

 

Link to Accoreconsole.

https://forums.autodesk.com/t5/forums/replypage/board-id/130/message-id/444989

 

Like others I work metric so mm, m and Unitless can be relevant answer for a dwg.

0 Likes
Message 9 of 13

MrJSmith
Advocate
Advocate

@Sea-Haven I don't think you can read the Lunits via OBDX, or at least I could not find a method. I'd be very interested in learning how to do this as I currently used ACCORECONSOLE for those tasks which is slower and messier.

0 Likes
Message 10 of 13

Sea-Haven
Mentor
Mentor

Why is Accoreconsole slower ? You can do a Bat file that loops through a directory of dwg names that may be easier than writing a multi line approach.

 

Yes did find out Getvar is not supported in obdx there may be some other ways will keep looking. Found a couple of ideas but are more complex than using accoreconsole.

0 Likes
Message 11 of 13

daniel_cadext
Advisor
Advisor

Python took about 24 seconds to process 3 drawings 1500 times. I assume the drawings are blocks, so I used drawings around 200kb

Accoreconsole would be the way to go if it works. @Sea-Haven to you have an example processing one drawing, that would append the data to a file, .csv? I've never used Accoreconsole  before

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 12 of 13

MrJSmith
Advocate
Advocate

@Sea-Haven It is slower than OBDX method. It is way faster than normal ACAD scripts.

0 Likes
Message 13 of 13

Sea-Haven
Mentor
Mentor

Use file append. Just keeps adding to a file.

 

Looks like pthyon can do a getvar of a dwg, am I reading the code correct ? Or is it opening dwg as it goes ?

 

(setq fo (open (setq fname "d:\\acadtemp\\mydwgsfile.csv") "A"))

 

 

There is read and write SDK for Autocad dwg its ran at the C++ .NET level I am sure it could read the variables as it would just walk through the DWG database till it finds like "INSUNITS", think of a dxf dump of a dwg you can find LUNITS.

 

SeaHaven_0-1714626373045.png

 

So if you can find some one with the kit they could write you a program and I would expect very fast.

 

0 Likes