Author Topic: Update attributes from Excel  (Read 2187 times)

0 Members and 1 Guest are viewing this topic.

jp_lujan

  • Guest
Update attributes from Excel
« on: June 27, 2006, 01:39:43 AM »
Hello:

Somebody would know to say to me where I have problema.
1º To Import attributes from AutoCAD with the macro "Exttr" from Autodesk.
2º To export  with the macro "RestoreByHand" any modification that has made in the worsheet of excel to the drawing of AutoCAD it does not work

It's AutoCAD 2004.

I leave the code it in case they can help me, thanks beforehand.







Code: [Select]
Public acad As Object
Public mspace As Object
Public Excel As Object
Public AcadRunning As Integer
Public excelSheet As Object

'_______________________________________________________________________________________

Sub Extract()
    Dim sheet As Object
    Dim shapes As Object
    Dim elem As Object
    Dim Excel As Object
    Dim Max As Integer
    Dim Min As Integer
    Dim NoOfIndices As Integer
    Dim excelSheet As Object
    Dim RowNum As Integer
    Dim Array1 As Variant
    Dim Count As Integer
    Dim SHand As String
    Dim TStr As String
     
    Set Excel = GetObject(, "Excel.Application")
    Worksheets("Attributes").Activate
    Set excelSheet = Excel.ActiveWorkbook.Sheets("Attributes")
    excelSheet.Range(Cells(1, 1), Cells(5000, 100)).Clear
    excelSheet.Range(Cells(1, 1), Cells(1, 100)).Font.Bold = True
    Set acad = Nothing
    On Error Resume Next
    Set acad = GetObject(, "AutoCAD.Application")
    If Err <> 0 Then
        Set acad = CreateObject("AutoCAD.Application")
        MsgBox "Open the drawing file first and then rexecute!"
        Exit Sub
    End If
    acad.Visible = True
    Set doc = acad.ActiveDocument
    Set mspace = doc.ModelSpace
    RowNum = 1
    Dim Header As Boolean
    Header = False
    For Each elem In mspace
        With elem
            If StrComp(.EntityName, "AcDbBlockReference", 1) = 0 Then
                If .HasAttributes Then
                    Array1 = .GetAttributes
                    For Count = LBound(Array1) To UBound(Array1)
                        If Header = False Then
                            If StrComp(Array1(Count).EntityName, "AcDbAttribute", 1) = 0 Then
                                TStr = Array1(Count).TagString
                                excelSheet.Cells(RowNum, Count + 1).Value = TStr
                            End If
                        End If
                    Next Count
                    RowNum = RowNum + 1
                    For Count = LBound(Array1) To UBound(Array1)
                        TStr = Array1(Count).TextString
                        excelSheet.Cells(RowNum, Count + 1).Value = TStr
                    Next Count
                    SHand = elem.Handle
                    excelSheet.Cells(RowNum, Count + 1).NumberFormat = "@"
                    excelSheet.Cells(RowNum, Count + 1).Value = SHand
                    Header = True
                End If
            End If
        End With
    Next elem
    NumberOfAttributes = RowNum - 1

    Set acad = Nothing
End Sub

'_______________________________________________________________________________________

Sub RestoreByHand()
    Dim sheet As Object
    Dim shapes As Object
    Dim elem As Object
    Dim Excel As Object
    Dim Max As Integer
    Dim Min As Integer
    Dim NoOfIndices As Integer
    Dim excelSheet As Object
    Dim RowNum As Integer
    Dim Array1 As Variant
    Dim Count As Integer
    Dim SHand As String
    Dim tempObj As Object
     
    Set Excel = GetObject(, "Excel.Application")

   
    Worksheets("Attributes").Activate
    Set excelSheet = Excel.ActiveWorkbook.Sheets("Attributes")
   
Dim iLig As Integer
Dim ColHand As Long, LigHand1 As Long, LigHand2 As Long
       
    Set acad = Nothing
    On Error Resume Next
    Set acad = GetObject(, "AutoCAD.Application")
    If Err <> 0 Then
        Set acad = CreateObject("AutoCAD.Application")
        MsgBox "Open AutoCAD launch"
        Exit Sub
    End If
    acad.Visible = True
    Set doc = acad.ActiveDocument
    Set mspace = doc.ModelSpace
    RowNum = 1
    Dim Header As Boolean
    Header = False
   
    ColHand = 17
    LigHand1 = 1
    LigHand2 = 10000
    Dim iArray As Long
    Dim jL As Long
   
    iArray = 0
    For iLig = LigHand1 To LigHand2
       iArray = iArray + 1
       SHand = excelSheet.Cells(iLig, ColHand).Value

         If SHand <> "" Then
            Set tempObj = doc.HandleToObject(SHand)
            Array1 = tempObj.GetAttributes
               For jL = 0 To 40
               TStr = excelSheet.Cells(iLig, jL + 1).Value
               Array1(jL).TextString = TStr
               Next jL
         End If
                                                                   
    Next iLig
   
    Set acad = Nothing
End Sub

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: Update attributes from Excel
« Reply #1 on: June 27, 2006, 10:49:26 AM »
Code Free Baby!

I've done this using Express tools.

You can use filter to select just the blocks you're interested in.

Express Tools-->Blocks-->Export Attribute Information

~Manipulate your data in Excel, maintaining column and row integrity. Save file when finished.~

Express Tools-->Blocks-->Import Attribute Information

It's not code, but hopefully it's useful.

Let us know if you still need a code based solution.

Bob Wahr

  • Guest
Re: Update attributes from Excel
« Reply #2 on: June 27, 2006, 10:53:17 AM »
In what way is that code free?  True it's using presupplied code to do the task, but code nonetheless.

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: Update attributes from Excel
« Reply #3 on: June 27, 2006, 11:02:34 AM »
Well, code free in that no additional code needs to be written.

I suppose using a computer for anything other than a doorstop isn't code free... :-)

Now here at least we have free code. But that's a different topic all together.