.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

several vb.net issues

14 REPLIES 14
Reply
Message 1 of 15
Anonymous
855 Views, 14 Replies

several vb.net issues

Basically I'm building a Batch Script processor and my code loops through
file names in a listview. It all seems to work failry well but I've now
noticed that the dwg's open in read only mode. Very odd. My code is below.

Another question is can autocad accept multiple commands at once. Do I force
my app to wait for autocad to complete the commands I send before sending
more commands? i.e. I want to wait for the scripts to finish running before
my app tries to save the drawing.

TIA

Russ

CODE:::

For i As Integer = 0 To Me.ListView1.Items.Count - 1
'open the file
Me.sbpText.Text = "Opening file..."
file =
ApplicationServices.Application.DocumentManager.Open(Me.ListView1.Items.Item(i).Text,
False)

'process the script
Me.sbpText.Text = "Running script..."

'set filedia to 0
ApplicationServices.Application.SetSystemVariable("filedia", 0)
'file.SendStringToExecute("filedia ", True, False, True)
'file.SendStringToExecute("0 ", True, False, True)

'load the script
file.SendStringToExecute("script ", True, False, True)
file.SendStringToExecute("""" & Me.txtScript.Text & """" & vbCr,
True, False, True)

'set filedia to 1
ApplicationServices.Application.SetSystemVariable("filedia", 1)
'file.SendStringToExecute("filedia ", True, False, True)
'file.SendStringToExecute("1 ", True, False, True)

If file.IsReadOnly = True Then
MsgBox("file is readonly", MsgBoxStyle.Critical)
End If

'close the file
Me.sbpText.Text = "Saving & closing file..."

Try
file.CloseAndSave(Me.ListView1.Items.Item(i).Text)
Catch ex As Exception
MsgBox(ex.Source & " : " & ex.Message)
End Try

'advance the progress bar
spb.ProgressBar.Value = spb.ProgressBar.Value + 1
Next
14 REPLIES 14
Message 2 of 15
Anonymous
in reply to: Anonymous

I'm getting a little confused here guys. I was getting frustrated trying to
get my dll working correctly so I wrote an exe using Interop. Gues
what....it works.

When I tried to go back to my DLL with almost the same code, no joy.
FILEDIA gets set to 0 OK (I also wrap the looping code in a try catch
finally that sets FILEDIA back to 1). I know from stepping through the code
that everyline is processed but for some reason AutoCAD is not processing
the script and the modified date of DWG's I process doesn't change.

Any ideas?

Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common

Public WithEvents AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
Dim AcadDoc As Autodesk.AutoCAD.Interop.AcadDocument

Sub ProcessDwg(ByVal ThisDwg As String)
'open the file
AcadDoc.Application.Documents.Open(ThisDwg)
AcadDoc = AcadApp.ActiveDocument
'AcadApp.ZoomExtents()

'process the script
AcadDoc.SetVariable("filedia", 0)

AcadDoc.SendCommand("script" & vbCr & """" & Me.txtScriptFile.Text &
"""" & vbCr)

AcadDoc.SetVariable("filedia", 1)

'close the file
AcadDoc.Close(Me.chkSaveDWG.Checked, ThisDwg)
End Sub


Imports acadApp = Autodesk.AutoCAD.ApplicationServices.Application
Imports acadDoc = Autodesk.AutoCAD.ApplicationServices.Document

Dim file As acadDoc

Sub ProcessDwg(ByVal ThisDwg As String)

acadApp.DocumentManager.Open(ThisDwg)
file = acadApp.DocumentManager.MdiActiveDocument
'AcadApp.ZoomExtents()

'process the script
acadApp.SetSystemVariable("filedia", 0)

file.SendStringToExecute("script" & vbCr & """" & Me.txtScript.Text
& """" & vbCr, False, False, False)

acadApp.SetSystemVariable("filedia", 1)

'close the file
file.CloseAndSave(ThisDwg)
End Sub
Message 3 of 15
NathTay
in reply to: Anonymous

Try.

Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common

Public WithEvents AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
Dim AcadDoc As Autodesk.AutoCAD.Interop.AcadDocument

Sub ProcessDwg(ByVal ThisDwg As String)
'open the file
AcadDoc = AcadApp.Documents.Open(ThisDwg)
'AcadApp.ZoomExtents()

'process the script
AcadDoc.SetVariable("filedia", 0)

AcadDoc.SendCommand("script" & vbCr & """" & Me.txtScriptFile.Text &
"""" & vbCr)

Do Until AcadApp.GetAcadState.IsQuiescent = True
Loop

AcadDoc.SetVariable("filedia", 1)

'close the file
AcadDoc.Close(Me.chkSaveDWG.Checked, ThisDwg)
End Sub
Message 4 of 15
Anonymous
in reply to: Anonymous

thanks but that seems not to be working either.

wrote in message news:5051790@discussion.autodesk.com...
Try.

Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common

Public WithEvents AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
Dim AcadDoc As Autodesk.AutoCAD.Interop.AcadDocument

Sub ProcessDwg(ByVal ThisDwg As String)
'open the file
AcadDoc = AcadApp.Documents.Open(ThisDwg)
'AcadApp.ZoomExtents()

'process the script
AcadDoc.SetVariable("filedia", 0)

AcadDoc.SendCommand("script" & vbCr & """" & Me.txtScriptFile.Text &
"""" & vbCr)

Do Until AcadApp.GetAcadState.IsQuiescent = True
Loop

AcadDoc.SetVariable("filedia", 1)

'close the file
AcadDoc.Close(Me.chkSaveDWG.Checked, ThisDwg)
End Sub
Message 5 of 15
Anonymous
in reply to: Anonymous

project attached if anyone would be so kind as to take a look
Message 6 of 15
cgay
in reply to: Anonymous

Russ,

I reworked some stuff in your example. Take a look.

C

Let me know if this works for you. It worked with your included samples on my system.

Message was edited by: CougerAC
Message 7 of 15
Anonymous
in reply to: Anonymous

thanks for that and sorry for not replying sooner but I've been out of the
country. Some interesting and useful pointers in your comments but stiull
my script does not execute on my drawings.

Russ

wrote in message news:5053590@discussion.autodesk.com...
Russ,

I reworked some stuff in your example. Take a look.

C

Let me know if this works for you. It worked with your included samples on
my system.

Message was edited by: CougerAC
Message 8 of 15
cgay
in reply to: Anonymous

Russ,

Did the script run at all? It appears on closer inspection that the script is running, but there are errors in the script that prevent it from running successfully. Also, the script relies on the AutoCAD document satisfying some requirements. If these requirements are not met, the script cannot continue. For instance, if you try to load a drawing that doesn't contain any drawn objects, but contains unreferenced blocks that you want purged, the script will never execute the purge commands because the commands before try to select all objects, which when none are found, continues to prompt for a selection. You need to issue a cancel in the script before running the next command or else the script will not complete.

You say that the scripts aren't running. Does that mean "At all?".
Are their error messages?
Can you write a simpler script that inserts a circle or something at say point 0,0? Does this work for all documents?

Also, your script relies on a LISP file being loaded. So what happens if that LISP file is not found?

Try to issue the commands yourself and see if it follows the script exactly. I have not been able to successfully do this.

If you don't need the flexibility of changing the script file, consider adding code to the project that does what you want. You may find that you need to used both managed objects and Interop to get the job done, but you will be able to cope with errors, i.e. no objects to select, so skip operations that need selected objects.

Good Luck,
C
Message 9 of 15
Anonymous
in reply to: Anonymous

thanks,

I can't exactly tell if the script is running or not but the drawing doesn't
look like it should when complete. i've tried running the script files from
tools>run script... and the scripts run without error.

i tried commenting out the file.closeandsave line and run the dll again.
basically, the drawings appear to be opening as read only. i think now that
the scripts probably are running but the files aren't saving, either with
the qsave in the script or with the file.closeandsave line in the code.

russ


wrote in message news:5060035@discussion.autodesk.com...
Russ,

Did the script run at all? It appears on closer inspection that the script
is running, but there are errors in the script that prevent it from running
successfully. Also, the script relies on the AutoCAD document satisfying
some requirements. If these requirements are not met, the script cannot
continue. For instance, if you try to load a drawing that doesn't contain
any drawn objects, but contains unreferenced blocks that you want purged,
the script will never execute the purge commands because the commands before
try to select all objects, which when none are found, continues to prompt
for a selection. You need to issue a cancel in the script before running the
next command or else the script will not complete.

You say that the scripts aren't running. Does that mean "At all?".
Are their error messages?
Can you write a simpler script that inserts a circle or something at say
point 0,0? Does this work for all documents?

Also, your script relies on a LISP file being loaded. So what happens if
that LISP file is not found?

Try to issue the commands yourself and see if it follows the script exactly.
I have not been able to successfully do this.

If you don't need the flexibility of changing the script file, consider
adding code to the project that does what you want. You may find that you
need to used both managed objects and Interop to get the job done, but you
will be able to cope with errors, i.e. no objects to select, so skip
operations that need selected objects.

Good Luck,
C
Message 10 of 15
cgay
in reply to: Anonymous

Russ,
Just a stupid question on my part, are the Drawings marked as "Read Only" in explorer? I just can't duplicate what you are seeing on my end. After I made the changes to your original code that is. I did see this happen, but that was with your original code, and I chalked it up to running in the document context instead of the application context. Can you try some blank drawings and a script to draw a circle just to be sure?

C
Message 11 of 15
Anonymous
in reply to: Anonymous

tried on these drawings. no joy
Message 12 of 15
cgay
in reply to: Anonymous

Russ,

Here is what is happening.

1. User Clicks Process Button
2. Code loads document requested
3. SendStringToExecute is sent
4. Document Saves and Closes
5. SendStringToExecute completes

So, for some reason, sendStringToExecute doesn't actually happen until your current procedure is finished. A problem in my view. I think there are a couple of ways around this. Here is one solution using a Timer.

Rename frmMain.txt to frmMain.vb and replace the one in the project with this.

Don't know how this will work on a big script though. I am not sure if the IsQuiescent method will catch a script in progress or not. Give it a try.

Clif
Message 13 of 15
NathTay
in reply to: Anonymous

I have a program that I use SendCommand to run a script and using an IsQuiescent loop as per my earlier post works.

Regards - Nathan
Message 14 of 15
Anonymous
in reply to: Anonymous

That seems to work on the first drawing. the script executes but then there
is a error 506.

i'll play some more and post my results.

thanks.
Message 15 of 15
Anonymous
in reply to: Anonymous

any ideas why this code doesn't work then?

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost