Thay đổi màu hình chiếu trong Inventor trùng với màu Assembly

Bài viết sau đây hướng dẫn thay đổi màu các hình chiều trong Inventor trùng màu với các chi tiết Part trong Assembly, giúp bạn có thể phân biệt các chi tiết tốt hơn.

Gần đây, trong một khi đào tạo khóa học Inventor cho một công ty ở Bình Dương, CAMMECH nhận được một câu hỏi rất hay như sau:

“Sử dụng lệnh gì của Inventor để đổi màu các đường curve của các hình chiếu trong khi xuất bản vẽ Drawing  trùng với màu của các Part trong Assembly?”

CAMMECH đã nghiên cứu và tham khảo các tài liệu để đưa ra giải pháp sử dụng Macro trong Inventor để làm được điều này một cách tự động và đơn giản nhất.

Cách đổi màu hình chiếu thủ công trong Inventor

Nếu bạn làm thủ công thì đơn giản chọn vào chi tiết đó trên thanh Model và làm như sau:

  • Chuột phải chọn Select As Edge.

  • Tiếp tục chuột phải chọn Properties

  • Và chọn Color, chọn màu bạn muốn set.

Thay đổi màu hình chiếu trong Inventor trùng với màu Assembly

Sử dụng Macro để thay đổi màu của hình chiếu trong Drawing

Còn đoạn code VBA sau đây do CAMMECH tham khảo thì có thể làm được những công việc như sau:

  1. Ấn lệnh để kích hoạt và chọn hình chiếu bạn muốn đổi màu các đường nét.

  2. Sau đó kiểm tra xem các Part có trong hình chiếu đó và set màu cho nó:

  • Nếu Part đó chưa có màu thì dòng code sẽ chọn màu cho nó.

  • Nếu Part đã set màu thì nó sử dụng màu của Part đó.

Các màu sẽ được quản lý dựa trên Layer.

Bạn có thể coi hình ảnh minh họa dưới đây:

Thay đổi màu hình chiếu trong Inventor trùng với màu Assembly

Đoạn mã VBA để thay đổi màu của hình chiếu:

Dưới đây là đoạn mã VBA để thực hiện các công việc trên. Đoạn mã  với hàm API chính sử dụng thuộc tính DrawingView.DrawingCurves và cùng với sử dụng phương thức Sheet.ChangeLayer để thay đổi các Layer.

Nhưng bạn cứ copy đoạn mã này và sử dụng thôi, “mì ăn liền” đi, đừng nghĩ nhiều ^^

Public Sub ChangeLayerOfOccurrenceCurves()
   ' Get the active drawing document.
   Dim drawDoc As DrawingDocument
   Set drawDoc = ThisApplication.ActiveDocument

   ' Have the user select a drawing view.
   Dim drawView As DrawingView
   Set drawView = ThisApplication.CommandManager.Pick( _
                  kDrawingViewFilter, "Select a drawing view.") 

   Dim docDesc As DocumentDescriptor
   Set docDesc = drawView.ReferencedDocumentDescriptor 

   ' Verify that the selected drawing view is of an assembly.
   If docDesc.ReferencedDocumentType <> kAssemblyDocumentObject Then
      MsgBox "The selected view must be of an assembly."
      Exit Sub
   End If 

   ' Get the component definition for the assembly.
   Dim asmDef As AssemblyComponentDefinition
   Set asmDef = docDesc.ReferencedDocument.ComponentDefinition 

   ' Process the occurrences, wrapping it in a transaction so the
   ' entire process can be undone with a single undo operation.

   Dim trans As Transaction
   Set trans = ThisApplication.TransactionManager.StartTransaction( _
                               drawDoc, "Change drawing view color") 

   ' Call the recursive function that does all the work.
   Call ProcessAssemblyColor(drawView, asmDef.Occurrences)
   trans.End
End Sub



Private Sub ProcessAssemblyColor(drawView As DrawingView, _
                                 Occurrences As ComponentOccurrences)
   ' Iterate through the current collection of occurrences.
   Dim occ As ComponentOccurrence
   For Each occ In Occurrences
      ' Check to see if this occurrence is a part or assembly.
      If occ.DefinitionDocumentType = kPartDocumentObject Then
         ' ** It's a part so process the color. 

         ' Get the render style of the occurrence.

         Dim color As RenderStyle
         Dim sourceType As StyleSourceTypeEnum
         Set color = occ.GetRenderStyle(sourceType) 

         ' Get the TransientsObjects object to use later.
         Dim transObjs As TransientObjects
         Set transObjs = ThisApplication.TransientObjects 

         ' Verify that a layer exists for this color.
         Dim layers As LayersEnumerator
         Set layers = drawView.Parent.Parent.StylesManager.layers 

         Dim drawDoc As DrawingDocument
         Set drawDoc = drawView.Parent.Parent 

         On Error Resume Next
         Dim colorLayer As Layer
         Set colorLayer = layers.Item(color.Name) 

         If Err.Number <> 0 Then
            On Error GoTo 0
            ' Get the diffuse color for the render style.
            Dim red As Byte
            Dim green As Byte
            Dim blue As Byte 

            ' Create a color object that is the diffuse color.
            Call color.GetDiffuseColor(red, green, blue)
            Dim newColor As color
            Set newColor = transObjs.CreateColor(red, green, blue) 

            ' Copy an arbitrary layer giving it the name
            ' of the render style.

            Set colorLayer = layers.Item(1).Copy(color.Name)

            ' Set the attributes of the layer to use the color,
            ' have a solid line type, and a specific width.

            colorLayer.color = newColor
            colorLayer.LineType = kContinuousLineType
            colorLayer.LineWeight = 0.02
         End If
         On Error GoTo 0 

         ' Get all of the curves associated with this occurrence.
         On Error Resume Next
         Dim drawcurves As DrawingCurvesEnumerator
         Set drawcurves = drawView.DrawingCurves(occ)
         If Err.Number = 0 Then
            On Error GoTo 0 

            ' Create an empty collection.
            Dim objColl As ObjectCollection
            Set objColl = transObjs.CreateObjectCollection() 

            ' Add the curve segments to the collection.
            Dim drawCurve As DrawingCurve
            For Each drawCurve In drawcurves
               Dim segment As DrawingCurveSegment
               For Each segment In drawCurve.Segments
                  objColl.Add segment
               Next
            Next 

            ' Change the layer of all of the segments.
            Call drawView.Parent.ChangeLayer(objColl, colorLayer)
         End If
         On Error GoTo 0
      Else
         ' It's an assembly so process its contents.
         Call ProcessAssemblyColor(drawView, occ.SubOccurrences)
      End If
   Next
End Sub

Sau khi tạo được module cho đoạn mã VBA trên thì bạn có thể tạo một nút lệnh (Tool/Customize)  trong phần Drawing để tiện sử dụng.

Thay đổi màu hình chiếu trong Inventor trùng với màu Assembly

Bạn hãy thử ngay và coi thành quả, quá trình thực hiện nếu thắc mắc gì hãy liên hệ trung tâm CAMMECH để được giải đáp và hỗ trợ.

Chúc bạn thành công!

Nguồn tham khảo: https://modthemachine.typepad.com/my_weblog/2010/10/changing-drawing-curves-to-match-assembly-color.htm

Tại sao chọn Cammech

HỌC KÈM RIÊNG  ĐẾN KHI THÀNH THẠO RA NGHỀ

HỌC VIÊN TỰ CHỌN THỜI GIAN HỌC, LỊCH HỌC

LỚP MỞ HẰNG NGÀY, ĐĂNG KÝ LÀ ĐI HỌC NGAY

CHÚ TRỌNG KẾT HỢP LÝ THUYẾT VÀ THỰC HÀNH

BÀI GIẢNG CHUẨN QUỐC TẾ, MÁY MÓC HIỆN ĐẠI

ĐÁP ỨNG NHU CẦU HỌC CẤP TỐC, ONLINE TỪ XA

ƯU ĐÃI HỌC PHÍ DÀNH CHO HỌC SINH, SINH VIÊN

GIẢI ĐÁP THẮC MẮC, HỖ TRỢ HỌC LẠI MIỄN PHÍ

CẤP CHỨNG CHỈ KHÓA HỌC, HỖ TRỢ VIỆC LÀM

Tư vấn khóa học

(028) 66580151

0903111667

Chúng Tôi Sẵn Sàng Tư Vấn Khóa Học Cho Bạn

zalo
DMCA.com Protection Status