• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Distinguished Contributor
    VB_Autocad_guy
    Posts: 136
    Registered: ‎07-24-2009
    Accepted Solution

    Reload LineTypes

    392 Views, 9 Replies
    05-25-2012 03:29 PM

     

    How would I Reload a linetype... the linetype already exists in the drawing...

    But I want to update the linetype defintition. 

     

    This doesn't work. 

    I need to open the linetyperecord... but what do I use to reload or re-define the linetablerecord. 

     

    	Dim db As Database = ActiveDrawing.Database 
    			Dim ed As Editor = editor
    
    			Const filename As String = "SFR_LINETYPES.lin"
             
                Try
                 
    				Dim path As String = HostApplicationServices.Current.FindFile(filename, db, FindFileHint.Default)
    				db.LoadLineTypeFile("*", path)

     PLOTLINEWIDTH.png

     

    reload linetype.png

    Please use plain text.
    Distinguished Contributor
    Artvegas
    Posts: 104
    Registered: ‎04-21-2011

    Re: Reload LineTypes

    05-25-2012 10:06 PM in reply to: VB_Autocad_guy
    Came across a post over in the new AutoCAD DevBlog on this topic just yesterday. How to reload the line type in AutoCAD using AutoCAD.NET API: http://adndevblog.typepad.com/autocad/2012/03/how-to-reload-the-line-type-in-autocad-using-autocadne... Is this what you're looking for? Art
    Please use plain text.
    Moderator
    Alexander.Rivilis
    Posts: 1,178
    Registered: ‎04-09-2008

    Re: Reload LineTypes

    05-26-2012 02:35 AM in reply to: VB_Autocad_guy

    What about setting EXPERT system variable in value >= 3? 


    Пожалуйста не забывайте про Утвердить в качестве решения!Утвердить в качестве решения и Give Kudos!Баллы
    Please remember to Accept Solution!Accept as Solution and Give Kudos!Kudos

    Please use plain text.
    Distinguished Contributor
    VB_Autocad_guy
    Posts: 136
    Registered: ‎07-24-2009

    Re: Reload LineTypes

    05-29-2012 10:52 AM in reply to: Alexander.Rivilis

    Those solutions don't work. 

    I need to delete the linetype and then reimport or re-define it completely. 

    For whatever reason It's not working. 

     

    Autocad 2011 

    Windows XP

     

    Please use plain text.
    Valued Contributor
    FFlix
    Posts: 90
    Registered: ‎11-15-2011

    Re: Reload LineTypes

    05-30-2012 02:50 AM in reply to: VB_Autocad_guy

    using art's reference above, viru says that a drawing's line type definitions get updated when its .lin file is loaded (as you show in your post). therefore as long as your drawing continues to use its .lin file, the line type definition in the .lin file has to be changed (presuming the line type name stays the same)

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,371
    Registered: ‎10-08-2008

    Re: Reload LineTypes

    05-30-2012 03:57 AM in reply to: VB_Autocad_guy

    If CommanfFlags is set to Session

    you have use LockDocument, i.e.:

            [CommandMethod("relaodLinetype", CommandFlags.Session)]
    
            public static void relaodLinetype()
            {
                // Set full path of .LIN file herer:
                string linfile = @"C:\Test\SFR_LINETYPES.lin";
    
                DocumentCollection docManager = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
    
                Database db = docManager.MdiActiveDocument.Database;
    
                using (DocumentLock doclock = docManager.MdiActiveDocument.LockDocument())
                {
                    Transaction trans = db.TransactionManager.StartTransaction();
    
                    bool bReload = false;
    
                    using (trans)
                    {
    
                        LinetypeTable table = trans.GetObject(db.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;
    
                        if (table.Has("CENTER"))
    
                            bReload = true;
    
                    }
    
                    System.Int16 fileDia = (System.Int16)
    
                        Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("FILEDIA");
    
                    Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("FILEDIA", 0);
    
                    //reload using linetype command...
    
                    Object acadObject = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
    
                    object ActiveDocument =
                        acadObject.GetType().InvokeMember("ActiveDocument", System.Reflection.BindingFlags.GetProperty, null, acadObject, null);
    
                    object[] dataArry = new object[1];
    
                    if (bReload)
                    {
                        dataArry[0] = "-linetype Load CENTER\n" + linfile + "\nYes\n ";
                    }
                    else
                    {
                        dataArry[0] = "-linetype Load CENTER\nacad.lin\n ";
                    }
                    ActiveDocument.GetType().InvokeMember(
    
                                "SendCommand", System.Reflection.BindingFlags.InvokeMethod, null, ActiveDocument, dataArry);
    
                    Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("FILEDIA", fileDia);
                }
            }

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Distinguished Contributor
    VB_Autocad_guy
    Posts: 136
    Registered: ‎07-24-2009

    Re: Reload LineTypes

    05-30-2012 08:58 AM in reply to: Hallex

    Many Thanks Hallex, as you've helped me when I was in a pickle before. 

    Having some success here! :smileyvery-happy:

    It works!

     

    Two follow-up Questions: 

    1. 

     

    So why use the  

     ActiveDocument.[GetType]().InvokeMember("SendCommand", System.Reflection.BindingFlags.InvokeMethod, Nothing, ActiveDocument, dataArry)

     instead of 

       ActiveDrawing.SendStringToExecute("-linetype load....etc",False,False,False)

     Why use the InvokeMember? Does it work faster or better? What's the difference. 

     

    Could I just use: 

       Public Shared Sub SendCommand(ByVal Command As String)
                Dim doc As Autodesk.AutoCAD.ApplicationServices.Document
                doc = Application.DocumentManager.MdiActiveDocument
                doc.SendStringToExecute(Command, False, False, True)
    
    
                
            End Sub

     As my function/method for sending commands? What should I dig into to learn more about this? 

     

    2. What does LockDocument do exactly, (Yes obviously it locks the document) but why do it on the linetype load

    Please use plain text.
    Distinguished Contributor
    VB_Autocad_guy
    Posts: 136
    Registered: ‎07-24-2009

    Re: Reload LineTypes

    05-30-2012 09:07 AM in reply to: Hallex

    Oh for anybody following along here's my code; 

     

     <CommandMethod("reloadLinetype", CommandFlags.Session)> _
            Public Shared Sub Reload_LineType()
    
                ' Set full path of .LIN file herer:
                Dim linfile As String = gstrc_SFR_LineType_FullFileName
                Dim linName As String = "PNEUMATIC"
                Dim bReload As Boolean = False
    
                '' Get the current document and database
                Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
                Dim DB As Database = acDoc.Database
    
                Try
    
                    'Lock Document
                    Using DocLock As DocumentLock = acDoc.LockDocument
    
                        '' Start a transaction
                        Using TR As Transaction = DB.TransactionManager.StartTransaction
    
                            '' Open the Linetype table for read
                            Dim acLineTbl As LinetypeTable
                            acLineTbl = TR.GetObject(DB.LinetypeTableId, _
                               OpenMode.ForWrite)
    
                            If acLineTbl.Has(linName) = true Then
                                bReload = True 
                            End If 
    
                            ' Save the changes and dispose of the transaction
                            TR.Commit()
    
    
                        End Using
    
                    End Using
    
    
    
                    Dim fileDia As System.Int16 = CShort(Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("FILEDIA"))
    
                    Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("FILEDIA", 0)
    
                    'reload using linetype command...
    
                    Dim acadObject As [Object] = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication
    
                    Dim ActiveDocument As Object = acadObject.[GetType]().InvokeMember("ActiveDocument", System.Reflection.BindingFlags.GetProperty, Nothing, acadObject, Nothing)
    
                    Dim dataArry As Object() = New Object(0) {}
    
                    If bReload Then
                        dataArry(0) = "-linetype Load " & linName & vbLf & linfile & vbLf & "Yes" & vbLf & " "
                    Else
                        dataArry(0) = "-linetype Load " & linName & vbLf & "acad.lin" & vbLf & " "
                    End If
    
                    ActiveDocument.[GetType]().InvokeMember("SendCommand", System.Reflection.BindingFlags.InvokeMethod, Nothing, ActiveDocument, dataArry)
    
                    Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("FILEDIA", fileDia)
    
    
                    dataArry(0) = "regen" & vbLf 
                    ActiveDocument.[GetType]().InvokeMember("SendCommand", System.Reflection.BindingFlags.InvokeMethod, Nothing, ActiveDocument, dataArry)
                 
               
                Catch
                    Console.WriteLine("Error in LoadLineType")
                End Try
    
    
            End Sub

     

    Please use plain text.
    Distinguished Contributor
    Artvegas
    Posts: 104
    Registered: ‎04-21-2011

    Re: Reload LineTypes

    05-30-2012 09:32 AM in reply to: VB_Autocad_guy

    In reponse to VB_Autocad_guy...

     

    The InvokeMember() code is calling the COM interop's AcadDocument.SendCommand() method, except it is calling this using the .NET reflection library's late binding technique. You can google this for more details.

     

    The AcadDocument.SendCommand() method has the advantage of being executed straight away (i.e. synchronously) versus the Document.SendStringToExecute() method which is executed after your code has finished (i.e. asynchronously).

     

    Note that for the COM AcadDocument.SendCommand() method to work synchronously it must be called in the application context (i.e. CommandFlags.Session needs to be set).

     

    Calling this method using late binding is significantly slower than if you reference the interop dll directly in your project. However the advantage is that it will work for both 32-bit and 64-bit systems as well as different versions of AutoCAD - i.e. you don't have to create multiple builds just for a simple COM interop call every now and then.

     

    It's a nice trick to know.

     

    For some further background refer to these posts:

    http://through-the-interface.typepad.com/through_the_interface/2006/08/techniques_for_.html

    http://through-the-interface.typepad.com/through_the_interface/2011/11/generating-preview-images-for...

     

     

    Art

    Please use plain text.
    Distinguished Contributor
    Artvegas
    Posts: 104
    Registered: ‎04-21-2011

    Re: Reload LineTypes

    05-30-2012 09:39 AM in reply to: Artvegas

    Oh and if you are accessing a document from the application context (i.e. CommandFlags.Session) then you need to lock any document before you edit it, hence the DocumentLock.

    Please use plain text.