TheSwamp

Code Red => VB(A) => Topic started by: M-dub on September 21, 2004, 09:28:18 AM

Title: Rename & Delete Dwg File
Post by: M-dub on September 21, 2004, 09:28:18 AM
I just want to make sure this is possible...I don't want the code or anything.  I want to use this as a learning project.  It seems like it should be very easy.

- Existing drawing open in acad (filename = LBDE2761.DWG)
- Prefix that needs to be added to filename = BAE
- Save file as BAE-LBDE2761.dwg
- Delete original file

Just let me know that "yes, it's possible" or "you might have some trouble..."

Thanks,
Mike
Title: Rename & Delete Dwg File
Post by: Keith™ on September 21, 2004, 10:38:42 AM
in VBA it is entirely possible...
Title: Rename & Delete Dwg File
Post by: M-dub on September 21, 2004, 10:50:07 AM
Ok, as I've 'proven' before, I'm new to almost all forms of programming.  I'm using Visual Basic 6.0.
How do I add anything from AutoCAD to the components in the Toolbar?
You have to understand where I'm coming from.  I sometimes don't know what to look for help on.  I know what needs to be done and it makes complete sense to me, but I don't know what I have to do in VB to make it happen.
I've looked in the Object Browser and found nothing that will help me (that I know of anyway).
I think what I need to look for is the current file open (and with focus) in AutoCAD.  Am I on the right track?

<M-dub says to himself>(*Man, I hope they don't get too annoyed with my ignorance!*)
Title: Rename & Delete Dwg File
Post by: Keith™ on September 21, 2004, 11:39:19 AM
Do you need to write it in VB6?
It can be done right from the VBIDE if you want ...

But since you asked ...

IMHO the AC object should NEVER be added to a VB program except at runtime, otherwise you create problems that occur between the object model of different versions of AutoCAD....

So how do you do it???

I know you asked for no code, but in the interest of helping you in your endevour (plus I already use this function alot in VB) I will share it ...

Code: [Select]
'Function to find AutoCAD or load it if not found
Function GetAcadObject() As Object
'Get the AutoCAD.Application as defined in the registry
 Set GetAcadObject = GetObject(, "AutoCAD.Application")
 'if we could not get it, then it is not running so...
    If Err <> 0 Then
        Err.Clear
        'lets create it ..i.e. start AutoCAD
        Set GetAcadObject = CreateObject("AutoCAD.Application")
        If Err <> 0 Then
        'if we can't start it then notify the user
            MsgBox "Could not load or find AutoCAD.", vbExclamation
            End
        End If
    End If
End Function


The function call is used in this scenario:
Code: [Select]

 Dim AC as Object
 Set AC = GetAcadObject


Now this does not help you in programming the task at hand, because you do not have the object model loaded into VB6, to add it do this ...

In your VB6 program select Project->References
Scroll to the AutoCAD200xType Library and check the box

To be able to access the AutoCAD object model in VB6 define all of your variable types (i.e. Drawing, Line etc.) as the appropriate AutoCAD types. This includes the above example, rather than define AC as an object (which it should be in the final build) define it as AcadApplication. This will improve your ability to write code in the IDE. Once you have completed your coding and the program works properly, go back and put in generic types for the AutoCAD objects (i.e. As Object) and if you use any AutoCAD types for other variable DIMs change them to either a Variant or equivalent VB data type.

Now, there are some other issues that you probably will encounter, but I will let you tell me what they are and I will address them as the need arises.
Title: Rename & Delete Dwg File
Post by: M-dub on September 21, 2004, 11:43:45 AM
Thank you so much, Keith.
I'm going to give this a valiant effort and see what I can do.  I've actually switched to the VBIDE already. ;)
Thanks again, and I'll post my progress...
(No laughing!  I'm new to this!  ;) )
Title: Rename & Delete Dwg File
Post by: Keith™ on September 21, 2004, 11:50:58 AM
If you are not using forms, you can probably just cut and paste your VBA code into VB6. Then do a compile, see what problems arise and just address them.
Title: Rename & Delete Dwg File
Post by: M-dub on September 21, 2004, 11:56:57 AM
I am using a small form.  Thanks again Keith.  Just let me play for a bit now and I'll surely be back.
Title: Rename & Delete Dwg File
Post by: M-dub on September 21, 2004, 12:47:42 PM
:evil:  :roll:


I can't find out how to extract the current document name!  That should be as easy as pie!  Even just to display it in a msgbox...
Title: Rename & Delete Dwg File
Post by: Keith™ on September 21, 2004, 12:59:22 PM
AcadApplication.ActiveDocument.Name
Title: Rename & Delete Dwg File
Post by: M-dub on September 21, 2004, 01:01:42 PM
:oops: :oops:
I was sooo close many times!

Thanks buddy!  :oops:
Title: Rename & Delete Dwg File
Post by: M-dub on September 21, 2004, 02:06:02 PM
AcadApplication.ActiveDocument.SaveAs NewFile, ac2000_dwg

NewFile is a variable I set as a string.  When I get to this stage in the code, it tells me "Cannot save the document without a name."

What gives?
Title: Rename & Delete Dwg File
Post by: M-dub on September 21, 2004, 02:14:34 PM
never mind....
Title: Rename & Delete Dwg File
Post by: Keith™ on September 21, 2004, 02:22:58 PM
Uh .. ok glad to be of help .... I think ...
Title: Rename & Delete Dwg File
Post by: M-dub on September 21, 2004, 02:38:25 PM
Nope, I figured it out...
Now, I've got to figure out how to delete the original...
I'm gettin' there.  :)
Title: Rename & Delete Dwg File
Post by: M-dub on September 21, 2004, 02:48:36 PM
Preliminary...It works though!  Just not completely.

Code: [Select]

Dim CurrentFile As String
Dim CurrentPath As String
Dim NewFile As String
Dim Del As Boolean

Private Sub UserForm_Activate()
CurrentFile = AcadApplication.ActiveDocument.Name
CurrentPath = AcadApplication.ActiveDocument.Path
lblCurrent = CurrentFile
lblCurrentPath = CurrentPath
End Sub

Private Sub txtNewFile_Change()
cmdOK.Enabled = True
NewFile = txtNewFile.Text
End Sub

Private Sub cmdOK_Click()
    If chkDeleteOrig = False Then
        If MsgBox("Change " & CurrentFile & " to " & NewFile & " ?", VbMsgBoxStyle.vbYesNoCancel) = vbYes Then
        ActiveDocument.SaveAs CurrentPath & "\" & NewFile, ac2000_dwg
        End If
    End If
End Sub
Title: Rename & Delete Dwg File
Post by: M-dub on September 21, 2004, 02:58:38 PM
Ok, I'm having a hard time finding out how to delete the original file....any hints?
Title: Rename & Delete Dwg File
Post by: Keith™ on September 21, 2004, 03:50:29 PM
Ok ... I will tell you but you MUST BE CAREFUL ....
There is a potential for serious consequenses if you are not.

Code: [Select]

FileSystem.Kill "DriveLetterFullPathFileNameFileExtension"


It accepts wildcards "*" and "?" to specify multiple files.

For example ... this will delete ALL files in the root folder, so please don't try it ...
Code: [Select]

FileSystem.Kill "C:\\*.*"
Title: Rename & Delete Dwg File
Post by: M-dub on September 21, 2004, 03:53:46 PM
Cheers...
If all goes as planned, there should be no errors.  Fingers are always crossed though.

;)
Title: Rename & Delete Dwg File
Post by: Keith™ on September 21, 2004, 04:31:27 PM
Just make sure to verify the filename BEFORE you tell it to delete...