Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

what can we do on a closed file without opening it?

17 REPLIES 17
SOLVED
Reply
Message 1 of 18
danielFMN5S
965 Views, 17 Replies

what can we do on a closed file without opening it?

What range of operations can we do on a closed file with LISP without opening the file?
and how to do it? 
Of course, I will need to run the Lisp from another open file,
but the operation will be on a closed file.


For example,
I want to add layers,
delete layers,
freeze layers, 
search layers
delete a specific LAYOUT from one or multiple files,
unload specific XREF from a file.
but I don't want to open the files,
or it take a long time to open the file only cos there is one layout that make it heavy.

myb it can be divided between getting information about a closed file 
and do things on the closed file and saving the changes?

17 REPLIES 17
Message 2 of 18
komondormrex
in reply to: danielFMN5S

ObjectDBX can help open a dwg  file into memory and do with it a limited number of operations. the layers' are among them.

Message 3 of 18
danielFMN5S
in reply to: komondormrex

it can also make changes in the file ? 

Message 4 of 18
komondormrex
in reply to: danielFMN5S

sure, you can save the opened file with changes made.

Message 5 of 18
danielFMN5S
in reply to: komondormrex

can u give me an example how to search the layer "123" in a file,  without open the file? 

Message 6 of 18
komondormrex
in reply to: danielFMN5S

(setq acad_DBX_Object (vlax-create-object (strcat "ObjectDBX.AxDbDocument." (substr (getvar 'acadver) 1 2)))
	  layer_found nil
)
(vla-open acad_DBX_Object "your_drive:\\your_path\\your_file.dwg")
(vlax-map-collection (vla-get-layers acad_DBX_Object)  
	'(lambda (layer) (if (= "123" (vla-get-name layer)) (setq layer_found t)))
)
(if layer_found (alert "Layer \"123\" found") (alert "Layer \"123\" not found"))
(vla-release-object acad_DBX_Object)
Message 7 of 18
pendean
in reply to: komondormrex

@komondormrex Any chance you want to take a stab at the requested scope from the OP

 

pendean_0-1715179771990.png

 

Message 8 of 18
pendean
in reply to: danielFMN5S


@danielFMN5S wrote:

...there is one layout that make it heavy.


While you wait for your ask... Have you had a chance to try any of these solutions to fix the slow-to-open-a-layout issue yet

https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/Slow-performance-w... 

and possibly
 
Message 9 of 18
MrJSmith
in reply to: danielFMN5S

You can do all the layer changes via ObjectDB (not opening the file). I think you can remove XREFs, but I'd have to double check that. You can't edit layouts (names, delete, etc) in ACAD due to bugs. Works fine in BCAD though.

Message 10 of 18
paullimapa
in reply to: danielFMN5S

The delfromdwg.lsp code offers a function as an example using ODBX to open given dwg name & delete given xref name

For Example:
To delete from Drawing c:\autodesk\TestDwg.dwg Xref with name MyTest:
(delfromdwg (findfile"c:\\autodesk\\TestDwg.dwg") "MyTest")


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 11 of 18
danielFMN5S
in reply to: komondormrex

Thanks,
your code works fine.
However, I'm getting an error message: "error: no function definition: VLA-RELEASE-OBJECT."
I've already run (vl-load-com),
and I don't have issues with other VLA commands.
I'm using Civil 3D 2024.


@pendean 
I did the most from ure links,
but I'm facing this issue specifically with a layout
that I changed to "conceptual" and saved.
When I tried to open the file again,
it wouldn't open.
I narrowed down the problem to the layout being saved in conceptual mode.

Therefore, I was hoping to create some Lisp routines to help organize files
and potentially bypass some of these bugs.

@MrJSmith 

u said "You can't edit layouts (names, delete, etc) in ACAD due to bugs." 
Are these bugs still present in the 2024 version?

@paullimapa 
I ran the Lisp code you provided,
but it didn't work when I tried to detach the XREF from the file.
It gave me the error message: "my file" Does NOT Contain Blocks: [numbers.dwg] on Any Layer."

I'll take a closer look at it later
I might have entered the XREF name incorrectly?
the xref name is numbers.dwg
i also tried 
(delfromdwg (findfile "my file path") "numbers" nil)
cos (delfromdwg (findfile "my file path") "numbers") gave me "error: too few arguments"
my xref file is numbers.dwg

Message 12 of 18
Sea-Haven
in reply to: danielFMN5S

Not sure but why use findfile when you look at examples in teh code they have a full dwg path ? The Findfile may be causing the problem.

 

(delfromdwg (findfile"c:\\autodesk\\Floor Plan Sample.dwg") "CHAIR7" nil)
maybe
(delfromdwg "c:\\autodesk\\Floor Plan Sample.dwg" "CHAIR7" nil)

 

 

Message 13 of 18
daniel_cadext
in reply to: danielFMN5S

I think with layouts you must use the layout manager; I don’t think this is available in Autolisp, I could be wrong though.

.NET may be an option if you get stuck, or python if you’re feeling adventurous

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

def deleteLayouts(db : Db.Database, layouts):
    try:
        man =  Db.LayoutManager()
        token = man.setupForLayouts(db)
        for layout in layouts:
            if man.layoutExists(layout,db):
                man.deleteLayout(layout,db)
    finally:
        man.clearSetupForLayouts(token)
        
def layerStuff(db : Db.Database, layers):
    lt = Db.LayerTable(db.layerTableId())
    for name , id in lt.toDict().items():
        if name in layers:
            ltr = Db.LayerTableRecord(id, Db.OpenMode.kForWrite)
            ltr.setIsFrozen(True)
            
def databaseProcessor(path : str, bsave: bool):
    db = Db.Database(False, True)
    db.readDwgFile(path)
    db.closeInput(True)
    deleteLayouts(db,["Layout1", "Layout2"])
    layerStuff(db,["1_1_WALLS", "1_2_BASES_PLAN"])
    if bsave:
        db.saveAs(path)
        
def PyRxCmd_doit():
    try:
        databaseProcessor("E:/06457Submittal.dwg", True)
    except Exception as err:
        print(err)

 

 

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
Message 14 of 18
paullimapa
in reply to: Sea-Haven

yes...no need for findfile


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 15 of 18
paullimapa
in reply to: danielFMN5S

Ok, my mistake...I posted the wrong code. That one deletes Block Inserts on given Layer names but not Xrefs.

The attached DelXrfFromdwg.lsp should delete Xrefs on any Layer.

For Example:
To delete from drawing TestDwg.dwg Xref with name MyTest:

(DelXrfFromdwg "c:\\autodesk\\TestDwg.dwg" "MyTest")

Note: as @Sea-Haven replied there's no need for findfile

 

I know this works on plain AutoCAD 2024 but not sure if it'll work on Civil 3D 2024

As for "error: no function definition: VLA-RELEASE-OBJECT"

The actual function name should be: VLAX-RELEASE-OBJECT


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 16 of 18
MrJSmith
in reply to: danielFMN5S

@danielFMN5S Yes, I tested in 2024. These ObjectDB bugs have been in AutoCAD for 20-30 years. I have lost any hope of them ever being fixed. As mentioned by others, .NET or Python would probably work. Or pickup a copy of BCAD LT (assuming non-3D dwgs) for a couple hundred. For automation solutions, it is superior in every way.

Message 17 of 18
danielFMN5S
in reply to: paullimapa

Tnx a lot, it's working great!

This code detaches my Xref without opening the main file.
You're awesome!

P.S. You're right

The command was vlax, not vla.
When I used vlax, it worked perfectly.


@Sea-Haven 
yes, no need findfile, 
In the first Lisp that deletes blocks, they included findfile in the examples.
However, I tested the new Lisp Paul posted (detach Xref) with findfile
and it worked great.
But I agree with you, there's no need for findfile.

@daniel_cadext 
I'm sure Python for AutoCAD is a great tool,
and I'll definitely learn more about the process.
tnx a lot 

@MrJSmith 
So, I won't hold my breath for version 2025. 😉

Tnx a lot for everyone ure amazing

Message 18 of 18
paullimapa
in reply to: danielFMN5S

Glad that worked out for you…cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report