10 Replies - 2803 Views - Last Post: 19 June 2012 - 01:02 PM Rate Topic: -----

#1 Prasenjit087   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-June 12

Specific cast is not valid

Posted 14 June 2012 - 05:39 AM

I am Working on Teghax Component.Using This Component we want to find out Block title attribute of dwg file or autocad file in a client server socket application.

1> We get Block title attributes properly of dwg file in windows application form using the Get_DwgFileAttribute function().
2> We also get Block title Attributes properly in that way:-We take a class library where we take a class.in this class we use Get_DwgFileAttribute function().Then We access the particular class from another project

But when we use the same class(Keep same structure) in client server socket program.Then it gives an exception.i.e ,"Spaicfied cast is not valid"I am Not getting any reply form teghax developer..I attached exception screen sort.Give me Suggestion How to solve this Exception

Code given Bellow :
 Public Function Get_DwgFileAttribute(ByVal Sender As ClsTcpConnection, ByVal msgTag As Byte, ByVal message As String) As String
        Dictionary = GetHashtable(message)
        Dim varfilepath As String = CStr(Dictionary.Item("filePath"))
        Dim varFileBlockName As String = CStr(Dictionary.Item("fileBlockName"))
        Dim hashList As New Hashtable
        If varfilepath.ToUpper.EndsWith(".DWG") Then
            odaHost = New TeighaX.OdaHostApp
            acadApp = odaHost.Application
            acadDoc = acadApp.Documents.Open(varfilepath)
            For i = 0 To acadDoc.Database.Blocks.Count - 1
                If TypeOf acadDoc.Database.Blocks.Item(i) Is TeighaX.IAcadBlock Then
                    curBlock = acadDoc.Database.Blocks.Item(i)
                    If curBlock.IsLayout Then
                        If curBlock.Layout.Name = "Model" Then
                            For A = 0 To acadDoc.Database.Blocks.Item(i).Count - 1
                                If TypeOf acadDoc.Database.Blocks.Item(i).Item(A) Is TeighaX.IAcadBlockReference Then
                                    If acadDoc.Database.Blocks.Item(i).Item(A).name = varFileBlockName Then
                                        blockRef = CType(acadDoc.Database.Blocks.Item(i).Item(A), IAcadBlockReference)
                                        If blockRef.HasAttributes Then
                                         Try
                                                ' Dim x As String = blockRef.Handle
                                                'Dim Attrib As IAcadAttribute
                                                For Each Attrib In blockRef.GetAttributes                                                                                                                                                    hashList.Add(CStr(Attrib.TagString), CStr(Attrib.TextString))
                                                Next
                                            Catch ex As Exception
                                         End Try
                                        End If
                                    End If
                                End If
                            Next
                        End If
                    End If
                End If
            Next
        End If
        Return "Request Dwg Block Attribute ..."

    End Function


Suggest me how to fix this Exception...........

Is This A Good Question/Topic? 0
  • +

Replies To: Specific cast is not valid

#2 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: Specific cast is not valid

Posted 14 June 2012 - 11:10 AM

Your problem is on line 18. Your exception is telling you that you can't convert your Item object to an IAcadBlockReference object.

This post has been edited by BobRodes: 14 June 2012 - 11:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 Prasenjit087   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-June 12

Re: Specific cast is not valid

Posted 15 June 2012 - 12:10 AM

Thanks BobRodes for your time.

Excuse me for not being specific in my previous post, Actually the problem is on line 23, i.e.
 For Each Attrib In blockRef.GetAttributes


GetAttributes() in a method of IACadBlockReference class which returns an object. I am trying to cast that object into an IAcadAttributeReference type automatically to get its TagString and Textstring properties respectively.

As I mentioned previously, the method [above code] works in every other scenario other than a client server model. I have also tried the following:
 
Dim Attrib As IAcadAttributeReference
For Each Attrib In blockRef.GetAttributes
...



But I get the same casting exception. Is there anything I need to take notice in a client-server scenario?
Was This Post Helpful? 0
  • +
  • -

#4 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: Specific cast is not valid

Posted 15 June 2012 - 06:19 AM

The problem is that I don't know anything about the "Teigha" objects you're using. There probably is something you have to take notice of, or you wouldn't be getting the error. Sorry I can't help any further. Perhaps someone else has experience with this.
Was This Post Helpful? 0
  • +
  • -

#5 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: Specific cast is not valid

Posted 15 June 2012 - 06:48 AM

I was working with the same objects the other day, and got the same error. It's one of those mysterious errors that isn't actually what it says. In my case and some other cases I've found, there is a null value somewhere, so you should place lots of line breaks and see where a value is turning up null. It could be there, or it could be somewhere else.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Specific cast is not valid

Posted 15 June 2012 - 11:32 AM

The empty Catch ex As Expection is gobbling up any exceptions and errors. It a big flag waving say me code is cack and errors, I can't be bother to code it correctly. So just ignore them an continue on regardless.
Was This Post Helpful? 0
  • +
  • -

#7 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: Specific cast is not valid

Posted 15 June 2012 - 01:16 PM

Yea I tend to remove my try catch blocks cause I find it makes tracing a bug harder.
Was This Post Helpful? 0
  • +
  • -

#8 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: Specific cast is not valid

Posted 16 June 2012 - 11:03 AM

Does VS have the VS6 equivalent of "Break on all errors" which disables error handlers? I've never found it.
Was This Post Helpful? 0
  • +
  • -

#9 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: Specific cast is not valid

Posted 17 June 2012 - 08:23 AM

I'm not sure Bob. I've never been able to find one, though it would be nice some times.
Was This Post Helpful? 0
  • +
  • -

#10 Prasenjit087   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-June 12

Re: Specific cast is not valid

Posted 18 June 2012 - 12:37 AM

Thank you all for your replies. Sorry for replying this late. @trevster344, thank you! at least there is someone out there who have used this Component. I will try to find if there is any null values in the code and report back here.

Meanwhile I have tried the following:

 If blockRef.HasAttributes Then
      Try
        Dim aAttributes() As Object                     '-- Not working
        '  Dim aAttributes() As IAcadAttributeReference  -- Not working
        Dim entAttribute As IAcadAttributeReference
        aAttributes = blockRef.GetAttributes()
        For idx = 0 To UBound(aAttributes)
        entAttribute = aAttributes(idx)
        ' use entAttribute here for Tag and TextString property
        Next idx
      Catch ex As Exception
...
...



I have put a breakpoint on line If blockRef.HasAttributes... I can see a valid IACadBlockReference(the type of blockRef) on my watch window.

Let me search if there is a null value somewhere else..

thanks again.
Was This Post Helpful? 0
  • +
  • -

#11 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: Specific cast is not valid

Posted 19 June 2012 - 01:02 PM

        aAttributes = blockRef.GetAttributes()



I assume you supply GetAttributes with some kind of parameter somewhere?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1