06-30-2024
04:36 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
06-30-2024
04:36 AM
Hi, I think the problem was the position off the views, I have the code adjusted.
You can also adjust the spacing between the views.
If this answers your question, please use ACCEPT SOLUTION to assist other users later.
Also be generous with Likes! Thank you and enjoy!
Sub main ' Get the active assembly. Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument Dim AssyName As String = "framework:1" 'name of the SubAssy Dim TemplateName As String = "white.dwg" 'name of the templae Dim Scale As String = "1:1" 'scale Dim SpacingBetweenViews As Double = 5 ' Call the function that does the recursion. TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, AssyName, TemplateName, Scale, SpacingBetweenViews) End Sub Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer, Name As String, TemplateName As String, scale As String, SpacingViews As Double) ' Iterate through all of the occurrence in this collection. This ' represents the occurrences at the top level of an assembly. Dim DesignProject = ThisApplication.DesignProjectManager.ActiveDesignProject Dim DrawingTemplate As String = System.IO.Path.Combine(DesignProject.TemplatesPath, TemplateName) Dim oOcc As ComponentOccurrence For Each oOcc In Occurrences ' Check to see if this occurrence represents a subassembly ' and recursively call this function to traverse through it. If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then If oOcc.Name = Name Then Dim AssyDoc As AssemblyDocument = oOcc.Definition.Document Dim oDrawDoc As DrawingDocument = CreateDrawing(DrawingTemplate) CreateViews(oDrawDoc, AssyDoc, scale, SpacingViews) End If TraverseAssembly(oOcc.SubOccurrences, Level + 1, Name, TemplateName, scale, SpacingViews) End If Next End Sub Private Function CreateDrawing(DrawingDoc As String) As DrawingDocument Dim result As DrawingDocument = Nothing Try Dim oNewDrawDoc As DrawingDocument = Nothing oNewDrawDoc = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, DrawingDoc) result = oNewDrawDoc Catch ex As Exception MsgBox(ex.ToString, MsgBoxStyle.Critical, "CreateDrawing") End Try Return result End Function Private Sub CreateViews(ByVal oDrawDoc As DrawingDocument, ByVal oAssyDoc As AssemblyDocument, scale As String, SpacingViews As Double) Try 'Set a reference to the active sheet. Dim oSheet As Sheet = oDrawDoc.ActiveSheet ' Create the placement point object. Dim Point1 As Double = ((oSheet.Width) / 2) - 8 Dim Point2 As Double = ((oSheet.Height) / 2) - 6 Dim oPoint As Point2d oPoint = ThisApplication.TransientGeometry.CreatePoint2d(Point1, Point2) 'view 1 FrontView Dim oViewFront As DrawingView = CreateBaseView(oAssyDoc, oSheet, oPoint, True, "Front", ViewOrientationTypeEnum.kFrontViewOrientation) oViewFront.ScaleString = scale oViewFront.ViewStyle = DrawingViewStyleEnum.kShadedDrawingViewStyle Dim oBaseViewCenter As Point2d = oViewFront.Center Dim obaseViewX As Double = oBaseViewCenter.X Dim obaseViewY As Double = oBaseViewCenter.Y Dim ViewFrontheight As Double = oViewFront.Height Dim ViewFrontWidth As Double = oViewFront.Width Dim oViewTop = CreateProjectedView(oViewFront, oSheet, "Top", oViewFront.Center.Y + 10) Dim oViewSide = CreateProjectedView(oViewFront, oSheet, "Side",, oViewFront.Center.X + 10) Dim oViewIso = CreateProjectedView(oViewFront, oSheet, "Iso", oViewFront.Center.X + 10, oViewFront.Center.Y + 10) Dim ViewTopheight As Double = oViewTop.Height Dim ViewTopWidth As Double = oViewTop.Width Dim oViewTopPlace As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(obaseViewX, obaseViewY + (ViewFrontheight / 2) + (ViewTopheight / 2) + SpacingViews) oViewTop.Position = oViewTopPlace Dim ViewSideheight As Double = oViewSide.Height Dim ViewSideWidth As Double = oViewSide.Width Dim oViewSidePlace As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(obaseViewX + (ViewFrontWidth / 2) + (ViewSideWidth / 2) + SpacingViews, obaseViewY) oViewSide.Position = oViewSidePlace Dim ViewIsoheight As Double = oViewIso.Height Dim ViewIsoWidth As Double = oViewIso.Width Dim oViewIsoPlace As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oViewSide.Center.X + (ViewSideWidth / 2), oViewTop.Center.Y + (ViewTopheight / 2)) oViewIso.Position = oViewIsoPlace Catch ex As Exception MsgBox(ex.ToString, MsgBoxStyle.Critical, "CreateViews") End Try End Sub Private Function CreateBaseView(ByVal oAssyDoc As AssemblyDocument, oSheet As Sheet, oInsertionPoint2D As Point2d, Folded As Boolean, ByVal DrawingViewName As String, ByVal ViewOrientation As ViewOrientationTypeEnum) As DrawingView Dim result As DrawingView = Nothing Try ' Create a BaseView. Dim oBaseView As DrawingView oBaseView = oSheet.DrawingViews.AddBaseView(oAssyDoc, oInsertionPoint2D, 1, ViewOrientation, DrawingViewStyleEnum.kShadedDrawingViewStyle) oBaseView.Name = DrawingViewName result = oBaseView Catch ex As Exception MsgBox(ex.ToString, MsgBoxStyle.Critical, "CreateBaseView") End Try Return result End Function Private Function CreateProjectedView(ByVal baseView As DrawingView, sheet As Sheet, DrawingViewName As String, Optional height As Double = 0, Optional width As Double = 0) As DrawingView Dim result As DrawingView = Nothing Try Dim oPositionBaseView As Point2d = baseView.Position Dim xBaseView As Double = oPositionBaseView.X Dim yBaseView As Double = oPositionBaseView.Y Dim oPointBottomView2 As Point2d oPointBottomView2 = ThisApplication.TransientGeometry.CreatePoint2d((xBaseView + width * 3), (yBaseView + height * 1.5)) 'BottomView Dim oProjected As DrawingView oProjected = sheet.DrawingViews.AddProjectedView(baseView, oPointBottomView2, DrawingViewStyleEnum.kShadedDrawingViewStyle) oProjected.Name = DrawingViewName result = oProjected Catch ex As Exception MsgBox(ex.ToString, MsgBoxStyle.Critical, "CreateProjectedView") End Try Return result End Function