Copy two views to another sheet (base view + projected view)

Copy two views to another sheet (base view + projected view)

matt_jlt
Collaborator Collaborator
522 Views
5 Replies
Message 1 of 6

Copy two views to another sheet (base view + projected view)

matt_jlt
Collaborator
Collaborator

Does anyone know how to copy a base view and a projected view to a new sheet?

 

I can copy a single view with no issues, but can not work out how to do it when there are multiple views. And copying them one at a time does not work.

 

I know i can probably use copy and paste commands but this is not a good solution. I would prefer an API based solution if possible.

 

Current code to copy one sheet only

oView.CopyTo(oSheet)

 

Thanks, Matt.

0 Likes
523 Views
5 Replies
Replies (5)
Message 2 of 6

petr.meduna
Advocate
Advocate

Which version of Inventor do you use? I've just tried it in 23 and it worked pretty well. Can you attach your file?

Dim draw As DrawingDocument = ThisApplication.ActiveDocument
For Each oView As DrawingView In draw.ActiveSheet.DrawingViews
	If oView.Name = "main" or oView.Name = "projected" Then
		oView.CopyTo(draw.Sheets.Item(2))
	End If
Next

 

Message 3 of 6

matt_jlt
Collaborator
Collaborator

Sorry, i should have clarified. Yes, that does work, but it doesnt keep the association between views when it copies them.

 

For example, if i have a base view + a projection view and a section view of the same part, i can't use that to copy all three. it wont copy the section with the base and the elevation view is copied but has no link the base anymore.

0 Likes
Message 4 of 6

petr.meduna
Advocate
Advocate

Yeah, section views cannot be copied. I've created simple function for "copying" section and for align projected view, see below:

Sub main()
Dim draw As DrawingDocument = ThisApplication.ActiveDocument
Dim newMainView As DrawingView, newProjectedView As DrawingView, newSectionView As DrawingView
For Each oView As DrawingView In draw.ActiveSheet.DrawingViews
	'make sure to copy base view first
	If oView.Name = "main" Or oView.Name = "projected" Or oView.Name = "A" Then
		Select Case oView.ViewType
			Case DrawingViewTypeEnum.kStandardDrawingViewType
				'copy main view
				newMainView = oView.CopyTo(draw.Sheets.Item(2))
			Case DrawingViewTypeEnum.kProjectedDrawingViewType
				'copy projected view
				newProjectedView = oView.CopyTo(draw.Sheets.Item(2))
				
				'call alignment sub
				AlignProjectedView(newMainView, newProjectedView)
			Case DrawingViewTypeEnum.kSectionDrawingViewType
				
				'call copysection function
				CopySectionView(newMainView, oView, draw.Sheets.Item(2))
		End Select
	End If
Next
End Sub

Private Function CopySectionView(ByVal newParent As DrawingView, ByVal oView As SectionDrawingView, ByVal targetSheet As Sheet) As DrawingView
	'add new sketch to copied base view
	Dim newSketch As DrawingSketch = newParent.Sketches.Add()
	
	'copy content of section line sketch to created sketch
	oView.SectionLineSketch.CopyContentsTo(newSketch)
	
	'create origin point of the original sectionview on new sheet
	Dim oPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oView.Position.X, oView.Position.Y)
	Dim secView As SectionDrawingView
	
	'add new section view to new sheet with values from original section view, but with new parent view and sketch
	If oView.FullSectionDepth Then
		secView = targetSheet.DrawingViews.AddSectionView(newParent, newSketch, oPoint, oView.ViewStyle, oView.Scale, oView.ShowLabel, oView.Name, , oView.FullSectionDepth)
	Else
		secView = targetSheet.DrawingViews.AddSectionView(newParent, newSketch, oPoint, oView.ViewStyle, oView.Scale, oView.ShowLabel, oView.Name, , oView.FullSectionDepth, oView.SectionDepth)
	End If
	
	'trycatch because of the aligned property, new section view will be created aligned to parent, so if original section is aligned, error will occure
	Try
		secView.Aligned = oView.Aligned
		secView.Position = oPoint
	Catch
	End Try
	secView.ShowEntireLine = oView.ShowEntireLine
	
	'check the direction of section view
	If secView.DrawingCurves.Count <> oView.DrawingCurves.Count Then
		secView.ReverseDirection()
	End If
	Return secView
End Function

Private Sub AlignProjectedView(ByVal newParent As DrawingView, ByVal newProjectedView As DrawingView)
	'round values of views positions, they not the same for inventor, even if it looks the same in inventor
	If Math.Round(newParent.Left, 4) = Math.Round(newProjectedView.Left, 4) Then
		newProjectedView.Align(newParent, DrawingViewAlignmentEnum.kVerticalViewAlignment)
	ElseIf Math.Round(newParent.Top, 4) = Math.Round(newProjectedView.Top, 4) Then
		newProjectedView.Align(newParent, DrawingViewAlignmentEnum.kHorizontalViewAlignment)
	End If
End Sub

 

Message 5 of 6

matt_jlt
Collaborator
Collaborator

Thanks for your assistance, but to copy it properly with all dimensions and all child views, it looks like i have to do it with the copy / paste commands.

0 Likes
Message 6 of 6

petr.meduna
Advocate
Advocate

I am not sure that copy/paste commands are valid in this case. 

I've created new functions to copy drawing dimensions, there is only linear dimensions copy but I hope you will understand what I mean and how to write to copy other types of dimensions. Copying drawing notes is similar.

I'm afraid, It has to be hard coded, because there is no copy function to section view in Inventor.

Sub main()
Dim draw As DrawingDocument = ThisApplication.ActiveDocument
Dim newMainView As DrawingView, newProjectedView As DrawingView, newSectionView As DrawingView
For Each oView As DrawingView In draw.ActiveSheet.DrawingViews
	'make sure to copy base view first
	If oView.Name = "main" Or oView.Name = "projected" Or oView.Name = "A" Then
		Select Case oView.ViewType
			Case DrawingViewTypeEnum.kStandardDrawingViewType
				'copy main view
				newMainView = oView.CopyTo(draw.Sheets.Item(2))
			Case DrawingViewTypeEnum.kProjectedDrawingViewType
				'copy projected view
				newProjectedView = oView.CopyTo(draw.Sheets.Item(2))

				'call alignment sub
				AlignProjectedView(newMainView, newProjectedView)
			Case DrawingViewTypeEnum.kSectionDrawingViewType

				'call copysection function
				CopySectionView(newMainView, oView, draw.Sheets.Item(2))
		End Select
	End If
Next
End Sub

Private Function CopySectionView(ByVal newParent As DrawingView, ByVal oView As SectionDrawingView, ByVal targetSheet As Sheet) As DrawingView
	'add new sketch to copied base view
	Dim newSketch As DrawingSketch = newParent.Sketches.Add()

	'copy content of section line sketch to created sketch
	oView.SectionLineSketch.CopyContentsTo(newSketch)

	'create origin point of the original sectionview on new sheet
	Dim oPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oView.Position.X, oView.Position.Y)
	Dim secView As SectionDrawingView

	'add new section view to new sheet with values from original section view, but with new parent view and sketch
	If oView.FullSectionDepth Then
		secView = targetSheet.DrawingViews.AddSectionView(newParent, newSketch, oPoint, oView.ViewStyle, oView.Scale, oView.ShowLabel, oView.Name, , oView.FullSectionDepth)
	Else
		secView = targetSheet.DrawingViews.AddSectionView(newParent, newSketch, oPoint, oView.ViewStyle, oView.Scale, oView.ShowLabel, oView.Name, , oView.FullSectionDepth, oView.SectionDepth)
	End If

	'trycatch because of the aligned property, new section view will be created aligned to parent, so if original section is aligned, error will occure
	Try
		secView.Aligned = oView.Aligned
		secView.Position = oPoint
	Catch
	End Try
	secView.ShowEntireLine = oView.ShowEntireLine

	'check the direction of section view
	If secView.DrawingCurves.Count <> oView.DrawingCurves.Count Then
		secView.ReverseDirection()
	End If
	
	'create dimensions
	For Each dimension As GeneralDimension In oView.Parent.DrawingDimensions.GeneralDimensions
		If IsDimensionInView(dimension, oView) Then
			CreateDimension(dimension, secView)
		End If
	Next
	Return secView
End Function

Private Sub AlignProjectedView(ByVal newParent As DrawingView, ByVal newProjectedView As DrawingView)
	'round values of views positions, they not the same for inventor, even if it looks the same in inventor
	If Math.Round(newParent.Left, 4) = Math.Round(newProjectedView.Left, 4) Then
		newProjectedView.Align(newParent, DrawingViewAlignmentEnum.kVerticalViewAlignment)
	ElseIf Math.Round(newParent.Top, 4) = Math.Round(newProjectedView.Top, 4) Then
		newProjectedView.Align(newParent, DrawingViewAlignmentEnum.kHorizontalViewAlignment)
	End If
End Sub

Private Function CreateDimension(ByVal original As GeneralDimension, ByVal newView As DrawingView) As GeneralDimension
	Select Case original.Type
		Case ObjectTypeEnum.kLinearGeneralDimensionObject
			Dim oInt1 As GeometryIntent = FindIntentInNew(original.IntentOne, newView)
			Dim oInt2 As GeometryIntent = FindIntentInNew(original.IntentTwo, newView)
			Try
				return newView.Parent.DrawingDimensions.GeneralDimensions.AddLinear(original.DimensionLine.MidPoint, oInt1, oInt2, original.DimensionType, original.ArrowheadsInside, original.Style, original.Layer)
			Catch
				Return Nothing
			End Try
		Case ObjectTypeEnum.kDiameterGeneralDimensionObject
'			Dim oInt As GeometryIntent = FindIntentInNew(original.Intent, newView)
'			Try
'				Return newView.Parent.DrawingDimensions.GeneralDimensions.AddDiameter(original.DimensionLine.MidPoint, oInt, original.ArrowheadsInside, original.LeaderFromCenter, original.SingleDimensionLne, original.Style, original.Layer)
'			Catch
'				Return Nothing
'			End Try
	End Select
End Function

'find intent on new section view
Private Function FindIntentInNew(ByVal geo As GeometryIntent, ByVal newView As DrawingView) As GeometryIntent
	Dim point1 As Point2d, point2 As Point2d, point3 As Point2d, point4 As Point2d
	Select Case geo.IntentType
		Case IntentTypeEnum.kPointEnumIntent
			For Each cur As DrawingCurve In newView.DrawingCurves
				Try
					point1 = cur.StartPoint
					If geo.PointOnSheet.IsEqualTo(point1) Then
						Return newView.Parent.CreateGeometryIntent(cur, cur.StartPoint)
					End If
				Catch
					point1 = Nothing
				End Try
				Try
					point2 = cur.CenterPoint
					If geo.PointOnSheet.IsEqualTo(point2) Then
						Return newView.Parent.CreateGeometryIntent(cur, cur.CenterPoint)
					End If
				Catch
					point2 = Nothing
				End Try
				Try
					point3 = cur.EndPoint
					If geo.PointOnSheet.IsEqualTo(point3) Then
						Return newView.Parent.CreateGeometryIntent(cur, cur.EndPoint)
					End If
				Catch
					point3 = Nothing
				End Try
				Try
					point4 = cur.MidPoint
					If geo.PointOnSheet.IsEqualTo(point4) Then
						Return newView.Parent.CreateGeometryIntent(cur, cur.MidPoint)
					End If
				Catch
					point4 = Nothing
				End Try
			Next
		Case IntentTypeEnum.kParameterIntent
'			For Each cur As DrawingCurve In newView.DrawingCurves
'				Try
'					point1 = cur.CenterPoint
'					If geo.Geometry.CenterPoint.IsEqualTo(point1)
'						Return newView.Parent.CreateGeometryIntent(cur)
'					End If
'				Catch ex As Exception
'					Messagebox.Show(Ex.Message & vbLf & ex.StackTrace)
'					point1 = Nothing
'				End Try
'			Next
	End Select
	Return Nothing
End Function

'Chceck if the dimension on sheet belongs to section view
Private Function IsDimensionInView(ByVal oDim As GeneralDimension, ByVal oView As DrawingView) As Boolean
	Dim b As Box2d = ThisApplication.TransientGeometry.CreateBox2d()
	b.MinPoint = ThisApplication.TransientGeometry.CreatePoint2d(oView.Left, oView.Top - oView.Height)
	b.MaxPoint = ThisApplication.TransientGeometry.CreatePoint2d(oView.Left + oView.Width, oView.Top)
	Select Case oDim.Type
		Case ObjectTypeEnum.kLinearGeneralDimensionObject
			Try
				If b.Contains(oDim.IntentOne.PointOnSheet) Then
					Return True
				Else
					Return False
				End If
			Catch
				Return False
			End Try
		Case ObjectTypeEnum.kDiameterGeneralDimensionObject
'			Try
'				If b.Contains(oDim.Intent.PointOnSheet) Then
'					Return True
'				Else
'					Return False
'				End If
'			Catch
'				Return False
'			End Try
	End Select
End Function
0 Likes