Excel VBA, AutoCAD ActiveX ¸Àº¸±â °ÁÂ(18) |
Excel°ú AutoCAD¿¬°á(3)
¿©ÀüÈ÷ extattr.xlsÀÇ ¸ðµâ¿¡ ÀÖ´Â Extract()ÇÔ¼ö ¼Ò½º¸¦ º¸°í ½ÃÀÛÇϵµ·Ï ÇÏ°Ú½À´Ï´Ù. "Attributes"¶ó´Â sheet¸¦ È°¼ºÈ(Activate)½ÃÅ°´Â °÷ ±îÁö ¼³¸í
Çß½À´Ï´Ù. ¿À´ÃÀº ±× ´ÙÀ½ÁÙºÎÅÍ º¸±â·Î ÇÏ°Ú½À´Ï´Ù.
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
Set excel = GetObject(, "Excel.Application")
Worksheets("Attributes").Activate
Set excelSheet = excel.ActiveWorkbook.Sheets("Attributes")
excelSheet.Range(Cells(1, 1), Cells(1000, 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
...ÀÌÇÏ»ý·«...
set excelsheet=excel.ActiveWorkbook.Sheets("Attributes") ¶ó°í µÇ¾îÀÖ½À´Ï´Ù. °³Ã¼(object)º¯¼ö¿¡ ÇÒ´çÇϱâ À§Çؼ setÀ» »ç¿ëÇß½À´Ï´Ù.
excelsheet¶ó´Â º¯¼ö¿¡ attributes¶ó´Â sheet¸¦ ÇÒ´çÇÏ°Ú´Ù´Â °ÍÀÌÁÒ. attributes½¬Æ®´Â Excel.activeworkbookÀÇ °³Ã¼À̱⠶§¹®¿¡ excel.activeworkbook.sheets()¸¦
ÀÌ¿ëÇؼ Á¢±ÙÇß½À´Ï´Ù.
´ÙÀ½ÁÙÀº excelsheet.range(cells(1,1),cells(1000,100)).clearÀÔ´Ï´Ù. excelsheet¿¡ ÀÖ´Â ragne()À» clearÇÏ°Ú´Ù´Â °ÍÀΰ¡º¾´Ï´Ù. clear¶ó´Â ¸Þ¼µå°¡
¾Æ¸¶µµ ÀÖ´Â ¸ð¾çÀÌÁö¿ä? ÀÌ·¸°Ô Áö·¹ÁüÀÛÀ¸·Î clear¸Þ¼µå´Â ´ë»ó range¸¦ Áö¿öÁÖ´Â ¸Þ¼µåÀ̱¸³ª.. ¶ó°í ¿¹ÃøÀÌ °¡´ÉÇÏÁö¿ä. oopÇÁ·Î±×·¥Àº object°¡
°¡Áö°í ÀÖ´Â ¼Ó¼º,¸Þ¼µå¸¦ ´Ù ¿Ü¿ï ¼ö´Â ¾ø±â ¶§¹®¿¡ ÀÌ·¸°Ô ¿¹Á¦¸¦ º¸°Å³ª ³²ÀÌ ¸¸µé¾îµÐ ÇÁ·Î±×·¥À» Àо¸ç ¼Ó¼º,¸Þ¼µå¸¦ ÀÍÇô¾ß µÉ Çʿ伺ÀÌ
ÀÖÀ» °Í °°½À´Ï´Ù. ±Íµ¿³ÉÀÌ Áß¿äÇÏ´Ù´Â À̾߱â°ÙÁö¿ä?
±× ¾Æ·¡ÁÙ¿¡ º¸¸é set acad = nothingÀ̶ó°í µÇ¾îÀÖ½À´Ï´Ù. acad´Â objectÇü º¯¼öÀε¥ °Å±â¿¡´Ù ¾Æ¹«°Íµµ ³ÖÁö ¾Ê°Ú´Ù. Áï ºñ¿ö³õ°Ú´Ù´Â °ÍÀÔ´Ï´Ù.
nothingÀ» ÀÌ¿ëÇÏ¿´½À´Ï´Ù.
´ÙÀ½ÁÙÀÌ On Error Resume Next¶ó°í µÇ¾îÀִµ¥, ÀÌ·± Äڵ带 ¿¡·¯Çڵ鷯¶ó°í ÇÕ´Ï´Ù. ´ëºÎºÐÀÇ ÇÁ·Î±×·¥µéÀº ¿¡·¯¸¦ ³»Æ÷ÇÒ ¼ö ÀÖ½À´Ï´Ù. ¹°·Ð ÇÁ·Î±×·¥
ÀÚü´Â ¿¡·¯°¡ ¾øÁö¸¸ »ç¿ëÁß¿¡ ¾ø´Â ÆÄÀÏÀ» ¼±ÅÃÇߴٵ簡, 0À¸·Î ³ª´©¾ú´Ùµç°¡ ±×·±°ÍÀº ÇÁ·Î±×·¥À» ¸¸µé¶§´Â ÀϾ ¼ö ¾ø´Â ¿¡·¯ÀÌ°í »ç¿ëÁß¿¡ ¹ß»ýÇÏ´Â
¿¡·¯ÀÔ´Ï´Ù. ÀÌ·¯ÇÑ ¿¡·¯µéÀ» °¨ÁöÇÏ°í ´ëÀÀÇÏ´Â °Íµµ ÇÁ·Î±×·¡¸ÓÀÇ ¸òÀÌ µÇ´Â °ÍÀÔ´Ï´Ù.
On Error Resume Next´Â "¾ÕÀ¸·Î ¿¡·¯°¡ ³ª¸é ±× ´ÙÀ½ÁÙ·Î ³Ñ¾î°¡¶ó.."¶ó´Â ¶æÀÔ´Ï´Ù. ¶Ç´Þ¸® ¸»Çϸé "¾ÕÀ¸·Î´Â ¿¡·¯°¡ ¹ß»ýÇÏ¸é ¸ØÃßÁö ¸»°í °è¼ÓÁøÇàÇ϶ó"¶ó°í
ÇÏ´Â °ÍÀÔ´Ï´Ù. ¾Æ¸¶µµ ´ÙÀ½¹®ÀåÀÌ ¿¡·¯°¡ ¹ß»ýÇÒ ¼ö ÀÖ´Â ¹®ÀåÀ̶ó´Â °ÍÀ» ¾Ë ¼ö ÀÖ½À´Ï´Ù. ´ÙÀ½¹®ÀåÀ» Çѹø º¸µµ·ÏÇÏÁö¿ä.
Set acad = GetObject(, "AutoCAD.Application") ¾Æ.. µåµð¾î AutoCAD¿Í ¿¬°áµÇ´Â ¼ø°£ÀÔ´Ï´Ù. ÀÁÙ¿¡¼ ¿¢¼¿À» excelº¯¼ö¿¡ ´ã¾Ò´ø °Íó·³
acadµµ acadº¯¼ö¿¡´Ù ´ã¾Ò½À´Ï´Ù. ¿ª½Ã object¸¦ ÇÒ´çÇϱâ À§Çؼ set¸í·É¾î¸¦ »ç¿ëÇÏ¿´±¸¿ä. AutoCAD ¸¦ Àâ¾Æ³»±â À§ÇØ getobjectÇÔ¼ö¸¦ »ç¿ëÇÏ¿´½À´Ï´Ù.
ÀÌÁ¦ acad¶ó´Â º¯¼ö¸¦ ÅëÇؼ AutoCADÀÇ ¸ðµç °³Ã¼¿¡ Á¢±ÙÇÒ ¼ö ÀÖ´Â °ÍÀÔ´Ï´Ù. °³Ã¼¿¡ Á¢±Ù¸¸ÇÏ¸é ±× °³Ã¼°¡ Áö¿øÇÏ´Â ÇÏÀ§°³Ã¼, ¼Ó¼º, ¸Þ¼µåµéÀ» »ç¿ëÇÒ
¼ö°¡ ÀÖ°ÚÁÒ?
¿©±â±îÁöÀÇ ÄÚµåÁß °¡ÀåÁß¿äÇÑ ºÎºÐÀº Set excel = GetObject(, "Excel.Application")¶ó´Â ÁÙ°ú Set acad = CreateObject("AutoCAD.Application")ÀÌ°ÚÁö¿ä?
ÀÌ µÎÁÙ¸¸ ¾Ë°í ÀÖÀ¸¸é ¿¬°áÀº ³¡³°ÍÀ̶ó°í ÇÏ°Ú½À´Ï´Ù.
ÀÌ·± ¿À´Ãµµ Á¡½É½Ã°£ÀÌ ´Ù µÇ¾ú½À´Ï´Ù. Áö±Ý±îÁö AutoCAD·Î ¿¬°áÇÏ´Â ´Ü°è¿´°í ³»ÀϺÎÅÍ´Â AutoCAD°³Ã¼ ¾Æ·¡¿¡ ÀÖ´Â µµ¸é,line,circle,text¿¡ Á¢±ÙÇÏ¿©
¸¸µé°í ¼öÁ¤ÇÏ´Â ¹æ¹ýÀ» ¼³¸íÇسª¾Æ°¡µµ·Ï ÇÏ°Ú½À´Ï´Ù. ±×·³ ÁÁÀº ³¯ µÇ½Ã°í ´ÙÀ½¿¡ ºË°Ú½À´Ï´Ù.
[ÀÌÀü °ÁÂ]
[´ÙÀ½ °ÁÂ]
|