6 Replies - 693 Views - Last Post: 04 June 2012 - 02:09 PM Rate Topic: -----

#1 Goodfix86  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 12
  • Joined: 01-July 09

C# / PDMWorks.dll Interop Search Program

Posted 04 June 2012 - 06:40 AM

I'm so close, yet so far away...

I am trying to write a search program to return drawings from the PDM Works vault. Solidworks expects you to do this in VB, but I am attempting it in C#.

I am able to connect, define the search options, pass the search options into the connection objects "Search" function and store the results into a PDMWorks.PDMWSearchResults object. The problem is that I can't seem to access the individual results programmatically.

There are a few examples on the web for VB that look similar to this:
cnt = Results.Count
For i = 0 To cnt - 1
   Set Result = Results(i)
   Worksheets(1).Cells(colcounter, i + 4).Value = Result.document.Name
Next i


First C# won't let me assign an index of Results to Result without specifying an explicit conversion.
Second I can expand the resultset in debug mode and see that my app is pulling in a collection, but every attempt so far to even write one of the properties contents has ended in ruin. It's not uncommon to see a VB example that reads "results(1).Name", but visual studio has no idea what to do with "results[0].Name" in C#.

This is what I have so far in C#:
static void Main(string[] args)
{
   PDMWorks.PDMWConnection pdmwVaultConn = new PDMWorks.PDMWConnection();
   PDMWorks.PDMWSearchResults results;
   
   pdmwVaultConn.Login("*****", "*****", "*****");
   
   var searchOptions = pdmwVaultConn.GetSearchOptionsObject();
   
   searchOptions.SearchCriteria.AddCriteria(PDMWAndOr.pdmwOr, PDMWPropertyType.pdmwDocumentName, "",
         PDMWConditionType.pdmwContains, "123456");
   
   results = pdmwVaultConn.Search(searchOptions);  
}



Returned values can be observed in the "Dynamic View" of an objects "Results View". Has anyone else done anything like this? Any tips on how to pull the information out of the Results collection? Again, there are plenty of VB examples on this. I want to do it with C#.

Is This A Good Question/Topic? 0
  • +

Replies To: C# / PDMWorks.dll Interop Search Program

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: C# / PDMWorks.dll Interop Search Program

Posted 04 June 2012 - 06:52 AM

If you have access to working VB.NET samples, then use them.
If nothing else you should be able to make a VB project that runs and works, then place breakpoints so you can see the exact data type things are coming in as. Then use that knowledge in your C# project.

Quote

It's not uncommon to see a VB example that reads "results(1).Name", but visual studio has no idea what to do with "results[0].Name" in C#.


I would really expect that to return a string. Its a name. 99% chance its a string.


I see a loop in the VB example, but no matching attempt in your C# example. So I'm confused. Have you tried to loop through the results or not? Your C# line 13 does successfully get results, right? Have you tried using var as the individual result?

for (int Index = 0; Index < Results.Count; Index++)
{
   var result = Results[Index];
   string TheName = result.Name;
}

Was This Post Helpful? 0
  • +
  • -

#3 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

Re: C# / PDMWorks.dll Interop Search Program

Posted 04 June 2012 - 07:02 AM

Have you used the debugger to see what is inside Results (values and datatypes)?
Was This Post Helpful? 1
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: C# / PDMWorks.dll Interop Search Program

Posted 04 June 2012 - 08:12 AM

My guess so far is that Results is of type object, since you haven't shown us its declaration.

Because of that, of course C# doesn't know what to do when you try to access properties that don't exist on an object. You'll have to cast it to the correct type to do that. The reason that VB would allow you to do that is because it allowed for late binding of members. C# does not (barring the dynamic type).

So, you have to know what type you're working with.
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: C# / PDMWorks.dll Interop Search Program

Posted 04 June 2012 - 08:54 AM

So I repeat... Make the VB project using the available examples you say are plentiful to learn what the eventual type(s) are. Then transpose that information to your C# project.
Was This Post Helpful? 0
  • +
  • -

#6 Goodfix86  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 12
  • Joined: 01-July 09

Re: C# / PDMWorks.dll Interop Search Program

Posted 04 June 2012 - 09:48 AM

I appreciate that you are trying to save me some grief by directing me to create a VB project, but at this point my curiosity has taken on a life of its own.

I figured it out and it was WAY simple and a little bit embarrassing for me. I had to typecast the index of PDMWSearchResults as a PDMWSearchResult. I thought I had already tried this, but I just did it and it worked.

static void Main(string[] args)
{
   PDMWorks.PDMWConnection pdmwVaultConn = new PDMWorks.PDMWConnection();
   PDMWorks.PDMWSearchResults results;
   PDMWorks.PDMWSearchResult result;

   pdmwVaultConn.Login("*****", "*****", "*****");

   var searchOptions = pdmwVaultConn.GetSearchOptionsObject();

   searchOptions.SearchCriteria.AddCriteria(
      PDMWAndOr.pdmwOr, 
      PDMWPropertyType.pdmwDocumentName, 
      "",
      PDMWConditionType.pdmwContains, 
      "123456");

   results = pdmwVaultConn.Search(searchOptions);

   result = (PDMWSearchResult)results[0];
   Console.WriteLine(result.Name);
}

The search results is of type PDMWSearchResults, but is passed back as System.__ComObject at runtime. I read a bunch of articles on reflection and other stuff that doesn't look fun so I'm happy that this works.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: C# / PDMWorks.dll Interop Search Program

Posted 04 June 2012 - 02:09 PM

View PostGoodfix86, on 04 June 2012 - 10:48 AM, said:

I thought I had already tried this, but I just did it and it worked.


Excerpt from my common tips:

Quote

[*]Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

Spoiler

If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1