News:

Be sure your post is relevant to the current discussion.  Create a new topic within the appropriate board if you are unsure.

Main Menu

VB Net get open TC app

Started by TedTheCarpenter, July 19, 2021, 09:21:45 AM

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

TedTheCarpenter

I'm a complete novice at VB Net. I'm trying to work with an open TurboCAD using the following:

Dim TC As IMSIGX.Application
TC = GetObject(, "TurboCAD.Application")

It returns TC=Nothing.

Dim TC As New IMSIGX.Application  does work but I'm trying to work with an existing and not a new instance of TurboCAD.

Any thoughts? Thanks.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

Andy H

Ted. 

First thing that comes to mind is,  Have you added the IMSIGX reference library into Visual studio.   It can be added via  VS - solution explorer pane.  (in my VS 2019, it is open to right side of VS).  Right click on 'references', and Add reference.  Use the bottom browse button to find IMSIGX.tlb.  Probably in C:\Program Files\IMSIDesign\TCW2021\SDK\Include  (I'm using 2021 platinum)

To reference a current open drawing. you also may need to add something like
TC_app = GetObject(, "TurboCAD.Application")
TC_drw = TC_app.ActiveDrawing

That said. Its been a long time since I did any of this stuff.

TedTheCarpenter

Andy,
I'm using VS2019 too and the project does have the reference to IMSIGX.
Dim TC As New IMSIGX.Application does create a new instance of TurboCAD but I'm trying to work with the active drawing of TurboCAD that's already open. From what I've read on the Web and the SDK forums GetObject(,"ClassName") is supposed to do that but there must be something wrong with my syntax. I've tried "TurboCAD.Application", "IMSIGX.Application", and several other variations as the Class Name but none of them work. The project is set to use Net 5.0 so it can use GetObject().
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

Andy H

My memory not good these days.

  I opened a new drawing in TC 2021.  and Just tried this in VS.

I created a form with a single button. and added the code for the button.  It draws a single 100mm polyline in the active drawing.

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim appl As IMSIGX.Application
        Dim drw As IMSIGX.Drawing
        Dim grf As IMSIGX.Graphic
        Dim verts As IMSIGX.Vertices
        Dim vx As IMSIGX.Vertex


        appl = GetObject("", "TurboCAD.Application")
        drw = appl.ActiveDrawing
        grf = drw.Graphics.Add(, "TCW50Polyline")
        verts = grf.Vertices
        vx = verts.Add(0, 0, 0)
        vx = verts.Add(100, 0, 0)
        grf.Draw()

        grf = Nothing
        drw = Nothing
        appl = Nothing

    End Sub
End Class

TedTheCarpenter

Andy,

I tried your code and got an exception at drw = appl.ActiveDrawing: "No Active Drawing Found". I do have TurboCAD running with an open drawing. The Task Manager shows that it creates a 2nd instance of TurboCAD as a background process and that instance obviously has no active drawing.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

Andy H

That is very strange.  I just tried again using an already open drawing, which had lots of stuff already in it.  The code works perfectly on my system.

Nikki has been doing some stuff with the SDK.  Hopefully she will comment on the situation.  Only thing I can think of, is if you can zip and post the VS file.  I've no idea if things like, running TC as admin would make any difference.   

TedTheCarpenter


Andy,

Running TC as an Administrator made no difference. I added a couple of lines after the GetObject:

        appl = GetObject("", "TurboCAD.Application")
        appl.Visible = True
        appl.Drawings.Add()

It shows that  GetObject is creating  a 2nd instance of TC. ActiveDrawing no longer throws an exception and the 100 mm line is drawn. But it's not doing it on my existing drawing.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

TedTheCarpenter

Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

Andy H

Unfortunately my version of VS throws an error using your VS file. It says that it cannot create the form.

However I did produce a problem similar to yours, using my existing file.  I closed 2021 and tried in v2019.  It didn't work.  So I closed 2019 and opened 2021.  My file failed, not finding the active drawing.  After closing 2021 again.  I found some of TC files (in task manager) had got left behind.  So I ended their task.  Restarted 2021, and My file now worked Ok Again. I then closed 2021, and opened 2019, That also now worked.

Therefore in my case.  Something in 2021 had caused some Task manager files to be left over after closing TC (a bit like the dwl files that can remain after a TC crash).  And these were preventing Active drawing from working. 

I cannot say you would find the same situation.  It is just what caused 'No active drawing' on my system. 

TedTheCarpenter

Here's the code I'm using for the Button1.Click event:

It throws the ActiveDrawing Exception with 2021 and 2019. It works fine with 2020. If I uncomment the two lines (appl.Visible & appl.Drawings.Add()) it opens a 2nd instance of 2021 and on 2020 it keeps the same instance but adds a new Drawing1 window. What explains this behavior is beyond me.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

Andy H

I had a play and cannot get it to fail in 2021 platinum.   Unless of course I ran the code whilst TC was not open.

The attached zip file is all the stuff produced by VS.  If you want to try it.  Unzip.  In VS File open project, navigate to ' Line_drawing_test1.sln ', and open.  The code is the same as the one you posted,  Plus.  I added a couple of message boxes,  The first will show every time the code is run.  the second, only if there is no active drawing.  And I added a few more vertices.  If one answers Yes to the first box, when TC is not open. the rouge processes will be created.

All I did was to create a new windows form,  paste in the code, did the minor alterations stated above.  Added IMSIGX reference, and it worked.   It works in the active drawing.  And I ever tried having two drawings open.  It drew the polyline in the correct one.

Sorry.  I'm out of ideas, as to what can be causing the problem for yourself.  NOTE I am using VS 2019, version 16.7.7.  And .Net version 4.8.04084,   I don't know if that could make a difference, but I wouldn't expect it to.

nikkipollard

#11
With version 2017 - I was using create object. It worked with an open Drawing.
But
I have been having the same problem since starting to use version 2021.
so I switched to GetObject which seems to work better with version 2021
I think Marek said that there was an issue with version 2017

'
Dim TCApp As IMSIGX.Application
  Dim TCDraw As IMSIGX.IDrawing
'connecting to the application copy this section to load form sub
   Try
          '  TCApp = CreateObject("TurboCAD.Application")
     TCApp = GetObject("", "TurboCAD.Application")
        Catch ex As Exception
            MsgBox("Can't connect to TurboCAD. Is TurboCAD running?")
            Exit Sub
        End Try
        TCApp.Visible = True
'linking to the active drawing or creating a new one
If TCApp.Drawings.Count > 0 Then
            TCDraw = TCApp.ActiveDrawing
        Else
'do nothing
MsgBox("No active drawing")
Exit Sub

'  or starts a new drawing
          '  TCDraw = TCApp.Drawings.Add("")

        End If
me.Topmost=True

I apologise for all the comments in the code they are notes to myself
Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

TedTheCarpenter

Andy,

My VS2019 is v16.10.3 and the .net is the same as yours. Line_drawing_test1 has the same result: It fails with 2021 and it works with 2020. If I uncomment the appl.Visible = True and  appl.Drawings.Add() lines it creates a 2nd instance of 2021. If I leave the lines commented Task Manager shows that it still creates a 2nd instance of 2021 but it remains a background process. I doubt that my slightly later version of VS is causing the different behavior on our computers. Perhaps it has something to do with our 2021 versions. I upgraded to Build 60.1   Are you still running the original Build?
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

TedTheCarpenter

Nikki,
On my computer your code works properly on 2018 and 2020. On 2019 and 2021 it creates a 2nd instance of TC that  has no ActiveDrawing.so it fails. Switching to CreateObject doesn't work either. Are you running the original version of 2021 or the update (Build 60.1)?
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

TedTheCarpenter

Perhaps the 2021 Build number is not the issue. My 2020 behaves differently than 2021 when I click on a .tcw file in Windows Explorer.
If 2020 is running and I open another drawing from Windows Explorer it opens the drawing as another window. In 2021 it opens the drawing in a 2nd instance of 2021. There might be an obscure setting in TC Explorer System Properties I could change to correct this behavior. This might explain why GetObject is behaving differently on our computers for 2021.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

Andy H

#15
Quote from: TedTheCarpenter on July 20, 2021, 08:59:59 AM
Andy,
Are you still running the original Build?

I'm running build 60.1

I never open a TC file from windows explorer.  But just tried, and it does open a new instance of TC 2021.  Though because the VS file works OK for me (and I assume Nikki), it suggests that is not the problem. 

I googled, and similar problems to yourself have existed with Excel since 2003.  But I couldn't find any specific cure.   One stated that both apps (VS and TC in this case), had to be either, both admin, or both non-admin.  Another suggested a problem accessing the Windows Running (registered) object table.    So VS has no idea that TC is actually running.  Though why there is a difference between 2020 and 2021, I've no idea.  And accessing the ROT is way beyond my programming skills. 

My spec.  TC 2021 platinum build 60.1 running RedSD,  (RedSDK would have no effect on TC instances).  Windows 10 v20H2  build 19042.  TC installed on default C:\ drive.  Both TC and VS are Non-admin.

TedTheCarpenter

Thanks Andy,
I'll look into the ROT. The following code showed that my running instance of 2021 was not hidden to VS even though it's invisible to GetObject:

        [Dim FILE_NAME As String
        Dim hnd As Long
        FILE_NAME = "C:\Running Apps and Window Caption.txt"
        Dim objWriter As New System.IO.StreamWriter(FILE_NAME, False)
        For Each clsProcess As Process In Process.GetProcesses()
            hnd = clsProcess.MainWindowHandle.ToInt64
            If hnd > 0 Then
                objWriter.WriteLine(clsProcess.ProcessName & "   " & clsProcess.MainWindowTitle)
            End If
        Next
        objWriter.Close()
        Application.Exit()
        End]
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

nikkipollard

#17
I don't think I know enough about programming to help out on this - I Still  just copy, paste and modify where needed.
just checking if we are using the same thing

I have had problems linking to some Turbocad Libraries - which seemed to be incompatible with what I was trying to do.

I mainly work with Imsigx24.dll . (Imsigx28.dll)
For Boolean Operations
gxext24.dll

This is what Marek wrote in another thread

"Hi Nikki
.......
"I also checked your VB script and it works as well. I just had to add to the script "TcApp = GetObject (," TurboCAD.Application ") because" TcApp = CreateObject ("TurboCAD.Application") ", does not allow to use the latest TC versions, only opens DC 5 for me, (DC5 provides the last 32 -bit version of TurboCADv4.1 Programmable Objects and this is the only reference that works on my 64-bit system.) I'll try to "play around" with this script later.
Marek"



Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

Andy H

Ted.

One last thing you could try is.   Send Vlad a PM (Personal message in the forum).  Click on his name in this thread to bring up his profile, where you can send a message.   Something like,  If he would be so kind as to have a look at this thread (provide a link to it) in the SDK section. and if he had any thoughts on the problem.

It won't send an E-mail as that function of the forum is broken, So it is 'pot-luck' if he sees the message on not.  But worth a try.   

Marek

Hi Ted  and guys
You can try this :

1.open TC 2021
2. open your WinFormsApp5 in VB
3. remove reference Interop.IMSIGX in VB
4.add reference Interop.IMSIGX in VB
5.run WinFormsApp5
Line should be drawn.
All apps,  I run as Admin.
Marek
Marek

TC Pro 2016, TC Pro 2018, TC Plat 2019,TC Plat 2020,TC Plat 2021,TC Plat 2022,TC Plat 2023,TC Plat 2024, TC Plat 2025
Laptop Asus i7 6500U, dual-core 2,50GHz, NVIDIA GeForce GTX 950M, RAM 12 GB, SSD 480GB
Windows 10 64 bit

TedTheCarpenter

Marek, I tried removing and reloading the IMSIGX reference (TC and VS were running as Admin) but no joy.
Andy, I did message Vlad. Thanks for the suggestion.

GetObject works fine on my laptop. Perhaps it's because the laptop is running Build 50.0 and my desktop has been updated to 60.1
Both computers have Windows 10 Pro Build 21H1 and SFC found no corrupt files on the desktop. Renaming the Built-in folder made no difference.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

Marek

I tried it on laptop with Win 10 20H2 64-bit. TC Plat 2021 , 64-bit, build 60.1.
Marek
Marek

TC Pro 2016, TC Pro 2018, TC Plat 2019,TC Plat 2020,TC Plat 2021,TC Plat 2022,TC Plat 2023,TC Plat 2024, TC Plat 2025
Laptop Asus i7 6500U, dual-core 2,50GHz, NVIDIA GeForce GTX 950M, RAM 12 GB, SSD 480GB
Windows 10 64 bit

TedTheCarpenter


I just solved the problem by uninstalling and reinstalling TC2021 on my desktop. GetObject is now returning the running instance of TC and ActiveDrawing is returning the open drawing. I have no idea what was wrong with my TC installation. I downloaded a second copy of 28.0.60.1 in case my original copy was corrupted. Hopefully I'll now be able to use the SDK to get beyond the limitations of PPM scripts. I really appreciate all the help and support I got from everyone!
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

nikkipollard

Quote from: Andy H on July 19, 2021, 10:55:51 AM
Ted. 

First thing that comes to mind is,  Have you added the IMSIGX reference library into Visual studio.   It can be added via  VS - solution explorer pane.  (in my VS 2019, it is open to right side of VS).  Right click on 'references', and Add reference.  Use the bottom browse button to find IMSIGX.tlb.  Probably in C:\Program Files\IMSIDesign\TCW2021\SDK\Include  (I'm using 2021 platinum)

To reference a current open drawing. you also may need to add something like
TC_app = GetObject(, "TurboCAD.Application")
TC_drw = TC_app.ActiveDrawing

That said. Its been a long time since I did any of this stuff.

Hi Andy do you know what the difference is between using the references in the Include folder and the .dll file suggested in the SDK documentation?
Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

Andy H

Quote from: nikkipollard on July 26, 2021, 02:10:30 AM

Hi Andy do you know what the difference is between using the references in the Include folder and the .dll file suggested in the SDK documentation?

Good question.  And simple answer is. I don't know.

In the include folder, it is just the type library (tbl reference) and its associated bits (header etc).  Containing information about TC object properties and methods, among other things. 

The dll contains the executable code.  However in TC and other programs, the dll also has the tbl file 'built in'.  So when using the dll as a reference, Visual Studio is accessing the tbl file inside the dll.  In theory it shouldn't make any difference which one is used in Visual Studio.   

As to why the tbl file is included as a separate file, when it is also built into the dll.  I don't know.  Its possible, at one time they needed to be separate.  As the imsigx header file is copyright 1997.  They probably just forgot to change the date, when updating the files.

type library contents


nikkipollard

Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

TedTheCarpenter


It turns out that reinstalling TC2021 did not solve the problem. The real issue is that GetObject("", "TurboCAD.Application") does not work if TurboCAD is run as an Administrator (it is if you get the UAC screen when TC starts). It works fine if you uncheck 'run as Administrator' in the exe file or shortcut properties. I wandered down more than a few blind alleys to find that out but now VB Net is a useful tool for me now.   ;D
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

nikkipollard

I am pleased to hear that.
I have an Excel worksheet with a whole lot VB syntax that I figured out . - If you interested
Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

TedTheCarpenter

Nikki,
I'm very interested and I'm sure other folks are as well. If you can, please send it as a zip file attachment. Thanks.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

nikkipollard

Here it is. -
I am not a programmer - so you might find easier ways to do things.
I add to it as I find things that work.
A lot of it is just copying syntax from examples on the forum. and out of Kevins help file.
Its all very basic I haven't experimented with making Tools or Regen objects yet

Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

TedTheCarpenter

Wow Nikki- that Excel file is an excellent resource! You're a lot more organized than I am. When I need to refresh my memory on how to accomplish something I have to read through a bunch of past coding that I finally got to work.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

Andy H

Quote from: nikkipollard on August 01, 2021, 07:21:09 AM
Here it is. -
I am not a programmer - so you might find easier ways to do things.


Very nice work Nikki.  Thanks for posting it.

TedTheCarpenter

#32
GetObject() fails when TC is running as Admin and the program using it is not. Here's the front end for a project that uses the Active Drawing. It checks to see if TC is running, if there are multiple instances of TC, if there is an Active Drawing, and if TC is running as Admin the the program is not. I've tested it on TC2018 to TC2021. It relies on TC displaying the drawing name within [ ] brackets in the Window Title so I'm not sure if it will work with versions earlier than TC2018. Make sure you add a COM reference to your IMSIGX dll or lib before running it. I used Visual Studio 2019 and .Net 5.0- it might have problems if you use an earlier version.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

nikkipollard

#33
Thanks Ted.
Silly question
How do you add a COM reference?
Edited  - figured that out, as a total green person
What is the difference between adding a reference and a com reference?
Only asking because I think it might solve some problems I am having.
Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

TedTheCarpenter

Hi Nikki

When you add a reference to a Visual Studio project you don't need to tell VS what type the component is. You browse to the file (.DLL, .TLB, .OLB, OCX, EXE, or .MANIFEST) and click the Add button. Once you add a TurboCAD reference (I used imsigx28.dll in Test GetObject) it will be listed under the COM tab because TC is still using that Legacy component format.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

nikkipollard

#35
In your project  your references have interop.........
mine don't

I added my references from the com Tab under references

What is the difference ? I think they are the same thing just viewed differently.
Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

TedTheCarpenter

If you have added the IMSIGX reference to the Test GetObject project you will need to press F7 to build a solution. Interop.IMSIGX.dll will then be placed in your obj\Debug (or Release)\net5.0-windows folder. If Dependencies\COM in Solution Explorer doesn't show Interop.IMSIGX you probably haven't done the Build step or the Build failed because of errors. My version of Visual Studio (16.10.4) will not show the References in Solution Explorer. If you have successfully added Interop.IMSIGX.dll press the Show All Files icon in Solutions Explored and navigate to the  obj\Debug (or Release)\net5.0-windows folder. You should see it there.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

nikkipollard

#37
Both projects work - I just wanted to know what the difference is.
I hardly ever have problems with running a program for TC. - and I have always used CreateObject

It makes sense if running as administrator is a problem - there might be some sort of  "safe mode" which limits external apps.
I noticed that VBA won't run a Turbocad script  if Turbocad has crashed since starting the excel program.
Every now and again I see a message from visual studio that there is a problem with dbautoserve.exe?? Restarting Turbocad and Visual studio fixes that.
Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

TedTheCarpenter

#38
I'm inclined to use GetObject  because CreateObject will start TC if it's not already running. The program will wait the considerable amount of time it takes  to open TC before telling you that there is no active drawing. I really have no idea why your project is not creating the Interop.IMSIGX.dll file- my VS always creates it when I add the IMSIGX reference. It does so if I use imsigx28.dll in the Program64 folder or imsigx.tlb n the Include folder. I've never had an issue with dbautoserve.exe but that's not saying much- I've only been using Visual Studio for a few weeks. I've been using VB6 for years but it's really getting long in the tooth so I'm transitioning to VB Net.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB

nikkipollard

I don't think it makes a difference it is probably a view setting in visual studio.
For me this is all new.
The only programming experience I had  was a few user forms in Excel.
I am guessing that VB.net is a lot like vb6. because VBA and VBscript are quite similar.


Nikki
TC20 platinum, TC 2015 platinum
TC 2017 with lightworks
TC 2021 platinum
Windows 11 Home 22h

TedTheCarpenter

You're right- a fair amount of VB Net is the same or very similar to VB6. But there are lots of differences too. I have a VB6 program that downloads points from my Total Station (surveying instrument) and imports them into TurboCAD (and the reverse so I can use the Total Station to layout footers,foundations and sill plates). I've been rewriting it as a VB Net program and it has taken me well over a day to learn the new way of reading and writing to the serial port.
Ted D

2017 Pro, 2018>2023 Platinum
Windows 11 Pro 22H2  64GB
Ryzen 9 5900X 12 core
Radeon RX 6600 XT 8GB